Hakim
Hakim

Reputation: 3437

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

Upvotes: 3

Views: 10011

Answers (3)

Febriyan
Febriyan

Reputation: 61

I like to use the following code in conf.py to know exactly what the current directory is and where is the target module (to obtain documentation):

current_dir = os.path.dirname(__file__)
target_dir = os.path.abspath(os.path.join(current_dir, "../../src"))
sys.path.insert(0, target_dir)

print(target_dir)

In this case, I'm looking to create documentation for my src, see the tree for context:

main
├── docs
│   ├── build
│   ├── make.bat
│   ├── Makefile
│   └── source
│       ├── conf.py
│       └── index.rst
│ 
└── src
    ├── __init__.py
    ├── target_module
    ├── requirements.txt
    └── setup.py

Next, from your terminal:

[user@localhost docs]$ sphinx-apidoc -f -o source/ ../src/target_module
[user@localhost docs]$ make html

Upvotes: 6

Plasma
Plasma

Reputation: 2439

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.

Upvotes: 5

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22942

Your current working directory should be the directory of your makefile, which should be docs.

Upvotes: 0

Related Questions