简体   繁体   English

Read the Docs Sphinx build 中不包含文档字符串

[英]Docstrings are not included in Read the Docs Sphinx build

I built a Sphinx documentation and the build works well locally.我构建了一个 Sphinx 文档,并且构建在本地运行良好。 My docstrings appear as shown below.我的文档字符串如下所示。

在此处输入图像描述

When moving to readthedoc.io, I added a specific requirement file under docs/requirement.txt which is:当转到 readthedoc.io 时,我在docs/requirement.txt下添加了一个特定的需求文件,即:

sphinx==3.5.4
sphinx_rtd_theme==0.5.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4

And my .readthedocs.yaml looks like:我的.readthedocs.yaml看起来像:

# Required
version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
   configuration: docs/source/conf.py

# Optionally build your docs in additional formats such as PDF
formats:
   - pdf

# Optionally set the version of Python and requirements required to build your docs
python:
   version: 3.7
   install:
    - requirements: docs/requirements.txt

However, when building the doc on readthedocs.io, even if my build completes with no error, the docstrings don't show.但是,在 readthedocs.io 上构建文档时,即使我的构建完成且没有错误,文档字符串也不会显示。 在此处输入图像描述

Here is the log:这是日志:

git clone --no-single-branch --depth 50 https://github.com/Green-Investement-Dashboard/GRID_app.git .
git checkout --force origin/app_v2
git clean -d -f -f
python3.7 -mvirtualenv /home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest
/home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest/bin/python -m pip install --upgrade --no-cache-dir pip setuptools
/home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest/bin/python -m pip install --upgrade --no-cache-dir mock==1.0.1 pillow==5.4.1 alabaster>=0.7,<0.8,!=0.7.5 commonmark==0.8.1 recommonmark==0.5.0 sphinx sphinx-rtd-theme readthedocs-sphinx-ext<2.2
/home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest/bin/python -m pip install --exists-action=w --no-cache-dir -r docs/requirements.txt
cat docs/source/conf.py
/home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest/bin/python -m sphinx -T -b html -d _build/doctrees -D language=en . _build/html
/home/docs/checkouts/readthedocs.org/user_builds/grid-app/envs/latest/bin/python -m sphinx -b latex -D language=en -d _build/doctrees . _build/latex
cat latexmkrc
latexmk -r latexmkrc -pdf -f -dvi- -ps- -jobname=grid-app -interaction=nonstopmode
mv -f /home/docs/checkouts/readthedocs.org/user_builds/grid-app/checkouts/latest/docs/source/_build/latex/grid-app.pdf /home/docs/checkouts/readthedocs.org/user_builds/grid-app/artifacts/latest/sphinx_pdf/grid-app.pdf

What have I done wrong?我做错了什么?

Remember that Sphinx's Autodoc extension "imports the modules to be documented".请记住,Sphinx 的Autodoc扩展“导入要记录的模块”。 That's because Autodoc uses Python introspection to access the doc-strings.这是因为 Autodoc 使用 Python 内省来访问文档字符串。 This has the advantage, from Autodoc's perspective, that it can let Python do the parsing.从 Autodoc 的角度来看,这样做的好处是它可以让 Python 进行解析。 The disadvantage, from a user perspective, is that we have to make sure all dependencies are installed, or at least "mocked".从用户的角度来看,缺点是我们必须确保所有依赖项都已安装,或者至少是“模拟的”。

This is not a problem on your development machine where, naturally, all external libraries your package depends on are already installed.这在您的开发机器上不是问题,当然,您的 package 所依赖的所有外部库都已安装。 But when building on the Read-the-Docs server, in a "naked" Python environment so to speak, many of them are missing.但是当在 Read-the-Docs 服务器上构建时,可以说是在“裸”Python 环境中,其中许多都丢失了。 You added the dependencies required for building the Sphinx project to docs/requirements.txt , and that would be sufficient if you didn't use the Autodoc extension.您将构建 Sphinx 项目所需的依赖项添加到docs/requirements.txt ,如果您不使用 Autodoc 扩展,这就足够了。 But since you do, your doc-strings are missing because modules in your package import something like flask_login or plotly .但是既然你这样做了,你的文档字符串就丢失了,因为 package 中的模块导入了类似flask_loginplotly的东西。 On Read-the-Docs, you should see warning messages to that effect if you look at the extended log (not the basic one you posted), which is accessible by clicking "View raw" in the Builds tab.在 Read-the-Docs 上,如果您查看扩展日志(不是您发布的基本日志),您应该会看到与此相关的警告消息,可以通过单击“构建”选项卡中的“查看原始日志”来访问该日志。 These are warnings, not errors.这些是警告,而不是错误。 So the build passes, but the doc-strings are missing.所以构建通过了,但是缺少文档字符串。

Adding the extra dependencies will slow down the documentation builds because they all have to be installed before Sphinx even gets to work.添加额外的依赖项会减慢文档构建速度,因为它们都必须在 Sphinx 开始工作之前安装。 So a better strategy is to mock them.所以更好的策略是嘲笑他们。 You can test that locally as well.您也可以在本地进行测试。

Packages which are imported like import numpy can be mocked by adding the Autodoc option autodoc_mock_imports to conf.py :可以通过将 Autodoc 选项autodoc_mock_imports添加到conf.py来模拟import numpy等导入的包:

autodoc_mock_imports = ['numpy']

If you import objects directly from a package's name space, like from numpy import array , it may be necessary to use MagicMock from the unittest module instead:如果您直接从包的名称空间导入对象,例如from numpy import array ,则可能需要使用unittest模块中的MagicMock

from unittest.mock import MagicMock
sys.modules['numpy'] = MagicMock()

Maybe you need to extend your path to your sources in your conf.py.也许您需要在 conf.py 中将路径扩展到源代码。

Like this for example (if your doc is in a subdirectory):例如像这样(如果您的文档在子目录中):

sys.path.insert(0, os.path.abspath("../"))

it would be easier to help you if you add your conf.py如果你添加你的 conf.py 会更容易帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM