3

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?

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • 1
    You can continue to keep adding to `sys.path`, but alternatively you could *install* the packages (i.e. using `setuputils.setup`). That way your packages will reside where they can be imported by any module. – Matt Messersmith Nov 14 '18 at 16:55
  • the actual (non MCVE) code is managed as part of a VSTO solution and is necessarily contained in its project folders/etc. Will that matter? – David Zemens Nov 14 '18 at 16:58
  • What does VSTO stand for? – Matt Messersmith Nov 14 '18 at 16:59
  • VSTO = Visual Studio – David Zemens Nov 14 '18 at 17:00
  • @mzjn if that was a problem, I'd be getting an actual error, not a warning from the `make` process. The paths, *exactly as given above* are producing the documentation as-expected. – David Zemens Nov 14 '18 at 17:13
  • @mzjn Right. Sphinx can't find the modules. This has nothing to do with whether I assign a raw string or an ordinary string. As given (not raw string), there are no warnings raised. My question is not about that. It's about what path(s) I should provide, or whether there's a way to do this without supplying *every* subpackage's full path. – David Zemens Nov 14 '18 at 17:40
  • It shouldn't matter if it's in VSTO. You have to have some way of doing the install, though (you can probably set it up so that you press f10 in VS and it works...but admittedly I am not familiar with VS). Colloquially `setup` is called from `setup.py`, usually like `python setup.py install` from shell. You can read more about it here: https://docs.python.org/3/install/index.html, and there are some SO answers about it too. – Matt Messersmith Nov 14 '18 at 18:19
  • 1
    And I meant `setuptools.setup`, not `setuputils` ;). Mixed up `distutils` and `setuptools` in the old brain... – Matt Messersmith Nov 14 '18 at 18:38
  • were you able to solve this? I'm having the same problem – LCsa Mar 26 '22 at 08:46
  • Honestly, no idea if I resolved this or not. That was 3+ years ago, sorry. – David Zemens Mar 31 '22 at 15:07

0 Answers0