我有带有NumPy文档字符串的Python代码。设法将Sphinx用于应用程序接口文档,但是,__init__.py文件中的类没有成功记录。
示例:xxx/__init__.py
from __future__ import annotations
import sys
import re
from typing import Iterator, ...
import pyparsing as p
__all__ = ['Xxx']
class XxxData:
"""It is a class XxxData."""
def __init__(self):
self.data = dict()
def get_foo(self, foo) -> str:
"""Get foo bar.
Parameters
----------
foo : str
It is just a string.
Returns
-------
str
It is just a string.
"""
return f'{foo} bar'
class Xxx():
"""It is a class Xxx."""
def __init__(self):
super().__init__()
self._something = 'something'
def parse_bar(self, bar) -> XxxData:
"""Parse bar.
Parameters
----------
bar : str
It is just a string.
Returns
-------
XxxData
It is a return object of XxxData.
"""
print(f'Hello {bar}')
data = XxxData()
return data示例conf.py
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))
extensions = [
'sphinx.ext.autodoc', # Core library for html generation from docstrings
'sphinx.ext.autosummary', # Create neat summary tables
'sphinx.ext.napoleon', # Support for NumPy and Google style docstrings
]
autosummary_generate = True # Turn on sphinx.ext.autosummary
autodoc_default_options = {
'show-inheritance': False,
'members': True,
'member-order': 'bysource',
'special-members': '__init__',
'undoc-members': True,
'exclude-members': '__weakref__'
}
autoclass_content = 'both'docs
|-Makefile
|-build
|-make.bat
|-source
|-_static
|-_templates
|-conf.py
|-index.rst
packages
|-pss
|-src/dd
|-ps
|-xxx
|-__init__.py下面的sphinx-apidoc命令自动创建了3个rst文件:source/api/ps.rst、source/api/ps.xxx.rst、source/api/modules.rst
sphinx-apidoc -o source/api/ ../packages/pss/src/dd/ps/ --implicit-namespaces -e -M -P示例source/api/ps.xxx.rst
ps.xxx package
==============
.. automodule:: ps.xxx
:members:
:undoc-members:
:show-inheritance:
:private-members:make html生成成功,但出现以下警告:
WARNING: autodoc: failed to import module 'xxx' from module 'ps'; the following exception was raised:
No module named 'ps.xxx'; 'ps' is not a package使用空内容呈现的HMTL页面。我希望看到来自__init__.py (上面的示例文件)的文档字符串被记录下来,但是没有发生。
发布于 2021-06-17 06:37:44
下面的sys.path适用于所有的子包/模块,除了只有__init__.py文件的模块。我使用../..是因为conf.py是顶级Python源文件夹下的2个层次结构。
sys.path.insert(0, os.path.abspath('../..'))通过显式地将../../packages/pss/src/dd附加到sys.path,已经根据问题的定义示例解决了只有__init__.py文件的模块的问题。
sys.path.insert(0, os.path.abspath('../../packages/pss/src/dd'))显然,必须处理所有报告的警告,才能使文档按预期呈现。
WARNING: autodoc: failed to import module 'xxx' from module 'ps'; the following exception was raised:
No module named 'ps.xxx'; 'ps' is not a packagehttps://stackoverflow.com/questions/67945351
复制相似问题