Sphinx autodoc找不到模块

时间:2019-09-02 23:38:30

标签: python python-sphinx

我是狮身人面像的新手,需要帮助弄清楚为什么我得到以下错误:

WARNING: autodoc: failed to import module 'employe_dao' from module 'models'; the following exception was raised:
No module named 'models'

我的项目结构是:

|--master_project
   |--sub_project
      |--docs
        |--build
        |--conf.py
        |--index.rst
        |--Makefile
        |--models.rst
        |--src.models.rst
        |--src.rst
      |--src
        |--models
          |--employee.py
          ...
        |--__init__.py
        |--data_extractor.py
        |--optimiser.py
    enter code here
        ...

这是index.rst的摘录

...

.. toctree::
   :maxdepth: 2
   :caption: Contents:

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

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

...

* :ref:`modindex`

我已根据Sphinx: autodoc can`t import module的建议在sys.path.insert(0, os.path.abspath('./sub_project'))中添加了import os, import sys并取消了注释conf.py

sphinx-build fail - autodoc can't import/find module @ryandillan建议将sys.path.insert(0, os.path.abspath('..'))添加到config.py修复了{{1}的model index 404“找不到索引” 错误}}

我已根据建议在另一个stackoverflow线程中将index.rst添加到extensions = ['sphinx.ext.autodoc']

关于我还有其他不正确操作的建议吗?

1 个答案:

答案 0 :(得分:2)

根据您的目录结构,要添加到sys.path的目录应为../src

要获得更一般的答案,请考虑一下,如果您想在Python CLI中成功import module,则需要位于哪个目录中。该目录就是您希望Sphinx在您的路径中的目录。