我有一个关于Sphinx autodoc生成的问题。我觉得我要做的应该很简单,但由于某些原因,它不会起作用
我有一个Python项目,其目录名为slotting\u tool。此目录位于C:\Users\Sam\Desktop\picnic-data-shared-tools\standalone\slotting_tool
我使用sphinx-quickstart
设置Sphinx。然后我的目录结构(简化)如下:
slotting_tool/
|_ build/
|_ source/
|___ conf.py
|___ index.rst
|_ main/
|___ run_me.py
现在,通过将以下内容添加到conf.py
文件中,我将项目的根目录设置为slotting_tool
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
接下来,我更新我的index.rst
文件,如下所示:
.. toctree::
:maxdepth: 2
:caption: Contents:
.. automodule:: main.run_me
:members:
当尝试使用sphinx-build -b html source .\build
命令构建我的html时,我得到以下输出,带有no module named
错误:
(base) C:\Users\Sam\Desktop\picnic-data-shared-tools\standalone\slotting_tool>sphinx-build -b html source .\build
Running Sphinx v1.8.1
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [] 0 added, 1 changed, 0 removed
reading sources... [100%] index
WARNING: autodoc: failed to import module 'run_me' from module 'main'; the following exception was raised:
No module named 'standalone'
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 1 warning.
The HTML pages are in build.
没有在内部版本中引用run_me.py
的HTML页面。我尝试过将我的根目录设置为所有不同类型的目录,并尝试过用反斜杠\
等替换所有点.
,但似乎无法发现我做错了什么
顺便说一下,standalone
不是模块的说法实际上是正确的,它只是一个没有__init__.py
的目录。不知道这是否会引起一些麻烦
有人有主意吗
那是不对的。Steve Piercy的评论并不完全正确(您不需要添加
__init__.py
,因为您使用的是一个简单的模块),但他们说autodoc将尝试导入模块,然后检查内容是正确的假设你的树是
然后,您只需将包含您的存储库的文件夹添加到sys.path,这是完全无用的。您需要做的是将
src
文件夹添加到sys.path,这样当sphinx尝试导入stack
时,它就会找到您的模块。所以你的路线应该是:(路径应相对于conf.py)
值得注意的是:由于你有一些完全合成的东西,并且不应该包含任何秘密,因此一个可访问的存储库或整个东西的zip文件可以使诊断问题和提供相关帮助变得更加容易:推断的越少,答案中的错误就越少
当源代码位于
src
目录(如Project/src
)中,而不是仅仅位于Project
基本目录中时,这是“入门”的常用“规范方法”。遵循以下步骤:
在
Project
目录中创建一个docs
目录(从该docs
目录执行以下步骤中的命令)sphinx-quickstart
(从build
中选择分开的source
。将.html
和.rst
文件放在不同的文件夹中)sphinx-apidoc -o ./source ../src
make html
这将产生以下结构(前提是
.py
源文件位于Project/src
):在
conf.py
中,您要添加(在步骤2之后):也包括在{}中:
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon']
在{}中,您可以链接{}:
您的
stack.rst
和modules.rst
是由sphinx-apidoc
自动生成的,此时无需更改它们。但你要知道这就是它们的样子:stack.rst
:modules.rst
:在浏览器中“make html”打开“Project/docs/build/index.html”后,结果如下:
以及:
让我们以一个项目为例:^{} on master branch ,提交:6cbcc2c72d5dc74d2defa56bf63706fd628d9892:
和utility package has a utils.py module:
遵循此过程(仅供参考,我正在使用斯芬克斯build 3.1.2):
docs/
目录:docs/
内启动狮身人面像,只需传递project_name
,your_name
&version
由您选择,其余保留默认值李>您将在
docs/
文件夹中自动生成以下内容因为,我们创建了一个单独的
docs
目录,所以需要sphinx查找 在哪里可以找到构建文件和python src模块。 因此,编辑conf.py文件,您也可以使用我的conf.py文件现在,要启用对嵌套多个包的访问&;如果有模块,则需要编辑
index.rst
文件modules
从我们将在下面创建的modules.rst
文件中拾取内容: 确保您仍然在doc/
中运行下面的命令您得到的输出:
现在运行:
现在,在您选择的浏览器中打开
file:///<absolute_path_to_your_project>/dl4sci-school-2020/docs/build/html/index.html
您准备好漂亮的文档了吗
https://imgur.com/5t1uguh
仅供参考,您可以切换您选择的任何主题,我发现
sphinx_rtd_theme
和扩展sphinxcontrib.napoleon
超级毒品!。感谢他们的创造者,所以我使用了它。下面的工作
您可以将文档托管在readthedocs 享受编写代码的乐趣
相关问题 更多 >
编程相关推荐