Sphinx autodoc 找不到模块

Sphinx autodoc can't find module

我是 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

中的建议在 conf.py 中添加 sys.path.insert(0, os.path.abspath('./sub_project')) 并取消注释 import os, import sys

sphinx-build fail - autodoc can't import/find module @ryandillan 建议将 sys.path.insert(0, os.path.abspath('..')) 添加到 config.py 修复了我的 404 "index not found" 错误 model index index.rst

我已按照另一个 Whosebug 线程中的建议将 extensions = ['sphinx.ext.autodoc'] 添加到 config.py

关于我做错了什么还有什么建议吗?

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

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