代码之家  ›  专栏  ›  技术社区  ›  David Zemens

Sphinx无法导入子包

  •  1
  • David Zemens  · 技术社区  · 6 年前

    我有一个项目目录,结构如下:

    |--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
    

    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'
    

    其他类似问题( 1 , 2 , 3 sys.path.insert 声明 conf.py 文件。

    我试过使用:

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

    但结果是一样的。

    如果我添加包的根路径( sys.path.insert(0, 'c:\debug\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'
    

    最终,我可以通过在 配置文件 文件:

    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 回复  |  直到 6 年前