Sphinx fails to import subpackages

Viewed 441

I have a project directory structured like so:

|--sphinx
   |--mypackage
     |--__init__.py
     |--core
        |--__init__.py
        |--program.py
        |--foo.py
     |--factory
        |--__init__.py
        |--basefactory.py

First, I run the sphinx-apidoc with the following paramters from command line:

c:\Debug\sphinx>sphinx-apidoc -o "C:\Debug\Sphinx" "C:\Debug\Sphinx\mypackage" -f --full

This creates the folder structure, .bat and .rst files, etc., within the "output" directory. Good so far.

enter image description here

When I try to run the make html, I get errors/warnings, indicating:

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'

Other similar questions (1, 2, 3) suggest modifying the sys.path.insert statement in the conf.py file.

I have tried using:

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

But the results are the same.

If I add the package's root path (sys.path.insert(0, 'c:\debug\sphinx'), I get fewer errors from Sphinx, this time only related to thee subpackages core and 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'

Ultimately, I can get this to produce the dox by inserting ALL of the paths in the conf.py file:

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

But it seems like I'm doing something wrong here. Is there a single path I can add to sys.path that will let this work without import errors? Should I have to hardcode all of the paths to package/subpackages? Or if not, what am I doing wrong?

0 Answers
Related