我使用的是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.rst
和docs/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
。