在构建sphinx文档时导入模块失败



我使用的是Sphinx版本的1.4.5

我的项目结构如下:

+ src > main.py + docs (generated with sphinx-quickstart)

即使在docs/conf.py中添加了src文件夹的路径:

sys.path.insert(0, os.path.abspath('../src'))

生成src/main.py(即docs/src.rstdocs/modules.rst)的第一个文件:

$ sphinx-apidoc -fo docs src

当我试图建立html网页:

$ make clean
$ make html

无法同时找到src模块和src/main.py模块:

WARNING: autodoc: failed to import module u'src.main'; the following exception was raised

我喜欢在conf.py中使用以下代码来确切地知道当前目录是什么以及目标模块在哪里(以获取文档):

current_dir = os.path.dirname(__file__)
target_dir = os.path.abspath(os.path.join(current_dir, "../../src"))
sys.path.insert(0, target_dir)
print(target_dir)

在这种情况下,我希望为我的src创建文档,请参阅上下文树:

main
├── docs
│   ├── build
│   ├── make.bat
│   ├── Makefile
│   └── source
│       ├── conf.py
│       └── index.rst
│ 
└── src
    ├── __init__.py
    ├── target_module
    ├── requirements.txt
    └── setup.py

下一步,从您的终端:

[user@localhost docs]$ sphinx-apidoc -f -o source/ ../src/target_module
[user@localhost docs]$ make html

对于路径插入尝试这样做:

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

还要考虑一个比src更好的目录名

您当前的工作目录应该是您的makefile的目录,应该是docs

最新更新