简体   繁体   English

Sphinx autodoc 找不到模块

[英]Sphinx autodoc can't find module

I am new to sphinx and need help to figure out why I am getting the following error:我是 sphinx 的新手,需要帮助来弄清楚为什么我会收到以下错误:

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

My project structure is:我的项目结构是:

|--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
        ...

This is a snipet from index.rst这是来自index.rst

...

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

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

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

...

* :ref:`modindex`

I have added sys.path.insert(0, os.path.abspath('./sub_project')) and uncomment import os, import sys in conf.py as recommened in Sphinx: autodoc can`t import module我添加了sys.path.insert(0, os.path.abspath('./sub_project'))并取消注释import os, import sys in conf.pySphinx 中推荐的那样:autodoc can`t import module

sphinx-build fail - autodoc can't import/find module @ryandillan suggestion to add sys.path.insert(0, os.path.abspath('..')) to config.py fixed my 404 "index not found" error for model index for index.rst sphinx-build 失败 - autodoc 无法导入/找到模块@ryandillan 建议将sys.path.insert(0, os.path.abspath('..'))config.py修复了我的404“未找到索引” index.rst model index错误

I have added extensions = ['sphinx.ext.autodoc'] to config.py as recommend in another stackoverflow thread.我已按照另一个 stackoverflow 线程中的建议将extensions = ['sphinx.ext.autodoc']config.py

Any suggestions on what else I am doing incorrectly?关于我做错了什么的任何建议?

Based on your directory structure, the directory to add to sys.path should be ../src根据您的目录结构,添加到sys.path的目录应该是../src

For a more general answer, think about what directory you would need to be in if you wanted to import module successfully in the Python CLI.有关更一般的答案,请考虑如果您想在 Python CLI 中成功import module您需要位于哪个目录中。 That directory is the one you want Sphinx to have in your path.该目录是您希望 Sphinx 在您的路径中拥有的目录。

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

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