我的项目目录结构如下:
|--sphinx
|--mypackage
|--__init__.py
|--core
|--__init__.py
|--program.py
|--foo.py
|--factory
|--__init__.py
|--basefactory.py
首先,我从命令行使用以下参数运行sphinx-apidoc
:
c:\Debug\sphinx>sphinx-apidoc -o "C:\Debug\Sphinx" "C:\Debug\Sphinx\mypackage" -f --full
这将在“输出”目录中创建文件夹结构,.bat和.rst文件等。到目前为止很好。
当我尝试运行make html
时,出现错误/警告,指示:
WARNING: autodoc: failed to import module 'mypackage'; the following exception was raised:
No module named 'mypackage'
WARNING: autodoc: failed to import module 'core.foo' from module 'mypackage'; the following exception was raised:
No module named 'mypackage'
WARNING: autodoc: failed to import module 'core.program' from module 'mypackage'; the following exception was raised:
No module named 'mypackage'
WARNING: autodoc: failed to import module 'core' from module 'mypackage'; the following exception was raised:
No module named 'mypackage'
WARNING: autodoc: failed to import module 'factory.basefactory' from module 'mypackage'; the following exception was raised:
No module named 'mypackage'
WARNING: autodoc: failed to import module 'factory' from module 'mypackage'; the following exception was raised:
No module named 'mypackage'
其他类似问题(1,2,3)建议修改sys.path.insert
文件中的conf.py
语句。
我尝试使用:
sys.path.insert(0, os.path.abspath)
但是结果是一样的。
如果我添加了包的根路径(sys.path.insert(0, 'c:\debug\sphinx')
,我从Sphinx得到的错误更少,这次仅与子包core
和factory
有关:
WARNING: autodoc: failed to import module 'program' from module 'mypackage.core'; the following exception was raised:
No module named 'foo'
WARNING: autodoc: failed to import module 'basefactory' from module 'mypackage.factory'; the following exception was raised:
No module named 'core'
最终,我可以通过在conf.py
文件中插入所有路径来生成dox:
sys.path.insert(0, 'c:\debug\sphinx')
sys.path.insert(0, 'c:\debug\sphinx\mypackage')
sys.path.insert(0, 'c:\debug\sphinx\mypackage\core')
sys.path.insert(0, 'c:\debug\sphinx\mypackage\factory')
但是看来我在这里做错了。我可以添加到sys.path
的单个路径,该路径可以在没有导入错误的情况下进行此工作吗?我是否必须对所有打包/子打包路径进行硬编码?否则,我在做什么错了?