在 Python Sphinx 中使用自动模块不显示变量和类属性
我想知道为什么在 Sphinx 中使用 automodule 指令时我无法看到类属性...即使属性有文档字符串。
与 Django 设置常量相同,它们没有显示。
我使用:
.. automodule:: settings
:members:
:show-inheritance:
:undoc-members:
我将设置拆分为模块
settings
- _init
- _.pyinstalled_apps.py
- locale.py
- db.py
- cache.py
- stage_stable.py
- stage_test.py
- stage_dev.py
- 。 ..
- templates.py
和 __init__.py
我从其他文件导入所有内容并选择我所在的阶段。
它适用于 Django,简化了设置修改并且...不适用于 Sphinx。
I am wondering why I am not able to see class properties when using automodule directive in Sphinx...Even if a propertie has a docstring.
Same thing with Django settings CONSTANTS, they don't shown.
I use:
.. automodule:: settings
:members:
:show-inheritance:
:undoc-members:
I split settings into module
settings
- _init_.py
- installed_apps.py
- locale.py
- db.py
- cache.py
- stage_stable.py
- stage_test.py
- stage_dev.py
- ...
- templates.py
and in __init__.py
I import everything from other files and select on which stage I am.
It works with Django, simplifies settings modification and... Does not work with Sphinx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档字符串通常不适用于类属性,但如果将其放在字段后面,Sphinx 的 autodoc 扩展就可以。您还可以在字段之前使用此特殊语法:
其他需要检查的事项是您是否在
conf.py
文件中列出了 autodoc 扩展名。查找extensions = ["sphinx.ext.autodoc"]
。 (该列表可能包含多个扩展名。)[编辑:] 我之前将文档注释放在了错误的位置。与文档字符串不同,
#:
注释必须位于您要注释的字段之前。[编辑:] 既然上面不是问题,那么还有另一种可能性。您在
.. automodule::
之后使用的模块或包必须可供您的文档访问。这意味着您需要确保将其位置添加到您的 Python 路径中。我的项目是这样设置的:在本例中,我需要将
/my_package
添加到 Python 路径,以便可以访问package
。为此,我确保它位于我的conf.py
的顶部:这有效地将
./../..
添加到 Python 路径,该路径来自 conf我的示例中的 .py 是my_project
目录。 (我还将其解析为绝对路径,这样出现意外的可能性就会减少。)显然,您必须根据您的具体情况更改此设置。我希望这对你有帮助。
docstrings normally don't apply to class properties, but the autodoc extension to Sphinx is able to if you put it after the field. You can also use this special syntax, before the field:
Other things to check are that you have the autodoc extension listed in the
conf.py
file. Look forextensions = ["sphinx.ext.autodoc"]
. (The list may contain more than one extension.)[edit:] I previously had the documentation comment in the wrong place. Unlike the docstring, the
#:
comments have to go before the field you are commenting.[edit:] Since the above isn't the problem, here's another possibility. The module or package you use after
.. automodule::
must be accessible to your documentation. This means you need to make sure you add its location to your Python path. My project is set up like this:In this case, I needed to add
/my_package
to the Python path so I could accesspackage
. To do so, I made sure this was in the top of myconf.py
:This effectively adds
./../..
to the Python path, which from conf.py in my example is themy_project
directory. (I also resolve it to an absolute path just so there are fewer possibilities for surprises.) Obviously, you'd have to change this for your specific case.I hope this helps you out.