简体   繁体   English

有没有一种方法可以预构建Sphinx中由autodoc生成的.rst文件

[英]Is there a way to prebuild the .rst files generated by autodoc in Sphinx

I have a project for which I've build a set of .rst files with sphinx.ext.apidoc that extracts documentation from my project's docstrings. 我有一个项目,我使用sphinx.ext.apidoc构建了一组.rst文件,该文件从项目的文档字符串中提取文档。

Those files look like the following: 这些文件如下所示:

Submodules
----------
.. toctree::
   mymodule.submodule

Module contents
---------------

.. automodule:: mymodule
   :members:
   :undoc-members:
   :show-inheritance:

make html builds the documentation properly on my computer, but requires me to edit the conf.py file to add my project to the python system path, so that it finds properly the modules mentioned in the .rst files when autodoc tries to import them. make html在我的计算机上正确构建了文档,但是要求我编辑conf.py文件以将我的项目添加到python系统路径中,以便当autodoc尝试导入.rst文件时,它可以正确找到.rst文件中提到的模块。

However, when I try to build the documentation on the readthedocs, autodoc is unable to find the referenced modules because I don't know what path needs to be added to the python system path for the autodoc to properly find the project modules. 但是,当我尝试在readthedocs上构建文档时,autodoc无法找到引用的模块,因为我不知道需要将什么路径添加到python系统路径中,autodoc才能正确地找到项目模块。

I was wondering if it would be possible to pre-build the .rst files with autodoc in the local environment, so that they don't contain any calls to autodoc anymore and then upload them to readthedocs, so that there is no need to run the autodoc extension there. 我想知道是否有可能在本地环境中使用autodoc预先构建.rst文件,以便它们不再包含对autodoc的任何调用,然后将其上载到readthedocs,从而无需运行那里的autodoc扩展名。

If there is no way to do this, what would be the proper way of solving that problem? 如果没有办法做到这一点,那么解决该问题的正确方法是什么?

After some tinkering, the solution was provided by the readthedocs FAQ : In order for the modules that depend on C and cannot be easily installed by pip inside the venv readthedoc builds, it is necessary to mock them and all sub-modules you import in the conf.py file within the myproject/docs/source directory of your documentation (or wherever your Sphinx's conf.py is). 经过一番修补后, readthedocs FAQ提供了该解决方案:为了使依赖C的模块无法通过venv readthedoc内部版本中的pip轻松安装,有必要对它们以及您导入的所有子模块进行模拟。 conf.py的内部文件myproject/docs/source您的文档(或任何你的狮身人面像的目录conf.py是)。 in my case the code was the following: 就我而言,代码如下:

if on_rtd:
    warn('debug -syspath -edit: %s'%os.path.abspath('../..'))
    sys.path.insert(0, os.path.abspath('../..'))

    class Mock(MagicMock):

        @classmethod
        def __getattr__(cls, name):
            return Mock()

        @classmethod
        def __getitem__(cls, name):
            return Mock()


    MOCK_MODULES = ['numpy',
                    'scikit-learn',
                    'pymongo',
                    'cython',
                    'Cython',
                    'matplotlib',
                    'matplotlib.pyplot',
                    'scipy',
                    'scipy.stats',
                    'scipy.sparse',
                    'scipy.sparse.linalg',
                    'scipy.sparse.csgraph',
                    'scikits',
                    'scikits.sparse',
                    'scikits.sparse.cholmod',
                    'sklearn',
                    'sklearn.cluster',
                    'python-Levenshtein'
                    'levenstein',
                    'python.levenstein'
                    ]

    for mod_name in MOCK_MODULES:
        sys.modules.update({mod_name: Mock()})

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

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