Sphinx无法导入子包

时间:2018-11-14 16:49:41

标签: python python-3.x python-sphinx

我的项目目录结构如下:

|--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文件等。到目前为止很好。

enter image description here

当我尝试运行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'

其他类似问题(123)建议修改sys.path.insert文件中的conf.py语句。

我尝试使用:

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

但是结果是一样的。

如果我添加了包的根路径(sys.path.insert(0, 'c:\debug\sphinx'),我从Sphinx得到的错误更少,这次仅与子包corefactory有关:

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单个路径,该路径可以在没有导入错误的情况下进行此工作吗?我是否必须对所有打包/子打包路径进行硬编码?否则,我在做什么错了?

0 个答案:

没有答案