h4k1m h4k1m - 3 months ago 13
Python Question

Failed to import module when building the sphinx documentation

I'm using

Sphinx
version
1.4.5
.

My project structure is the following:

+ src > main.py
+ docs (generated with sphinx-quickstart)


Even after adding the path to the
src
folder in
docs/conf.py
:

sys.path.insert(0, os.path.abspath('../src'))


And generating the rst file for
src/main.py
(i.e.
docs/src.rst
and
docs/modules.rst
) with:

$ sphinx-apidoc -fo docs src


When I try to build the
html
webpages with:

$ make clean
$ make html


It couldn't find both the
src
module and
src/main.py
:

WARNING: autodoc: failed to import module u'src.main'; the following exception was raised

Answer

Try doing this for your path insertion instead:

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

Also consider a better name for your directory than src.