在构建sphinx文档时无法导入模块

时间:2016-08-15 21:53:28

标签: python python-sphinx

我正在使用Sphinx版本1.4.5

我的项目结构如下:

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

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

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

3 个答案:

答案 0 :(得分:3)

尝试为您的路径插入执行此操作:

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

还要考虑比src更好的目录名称。

答案 1 :(得分:3)

我喜欢在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

答案 2 :(得分:0)

您当前的工作目录应该是您的makefile的目录,该目录应该是SELECT table1.mg_key, table1.mg_name, table1.time,--time= pick other name IFNULL(table2.mg_amount,0) + IFNULL(table3.mg_amount,0) as amount, IFNULL(table2.mg_key,0) + IFNULL(table1.mg_key,0) as pkey FROM table1 LEFT JOIN table2 ON table1.mg_key = key --key= reserved; pick other name LEFT JOIN table3 ON key = amount --key= reserved; pick other name