sphinx-build fail - autodoc can't import/find module
Autodoc can't find your modules, because they are not in sys.path
.
You have to include the path to your modules in in the sys.path
in your conf.py
.Look at the top of your conf.py
(just after the import of sys
), there is a sys.path.insert()
statement, which you can adapt.
By the way: you can use the Makefile
created by Sphinx to create your documentation.Just call
make
to see the options.
If something went wrong before try:
make clean
before running make html
.
solution
It sounds like os.path.append()
is working OK for folks, but if you follow the conf.py
template, you would insert the module path to the front of sys.path
using os.path.insert(0, ...)
, and just add an extra .
import osimport syssys.path.insert(0, os.path.abspath('..'))
If you have setup your sphinx
project to use separate build
and source
directories, that call should instead be:
sys.path.insert(0, os.path.abspath('../..'))
about sys.path ...
To run python code, the python interpreter needs to know where it is. With the sphinx config being a python script, it's location needs to be made known, which is done by adding it the the sys.path
variable using the insert method (see the docs on module search path).
The path added to sys.path
in this case is a "relative" path, which is specified using dots. This is a general way of specifying the path, which allows the code to be moved and still point correctly to the correct path in your codebase.
.
- current path of the conf.py
..
- parent path of the conf.py
../..
- parent of the parent path, etc.
I use linux, so directories are specified with a forward slash, but a cross-platform method for specifying parent directories can be acheived with pathlib
.
from pathlib import Pathparent = Path(__file__).parentparents_parent = Path(__file__).parents[1]
in conf.py
just add the path to your project folder.
sys.path.append('/home/workspace/myproj/myproj')