简体   繁体   中英

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 (ie 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

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 .

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

您当前的工作目录应该是您的 makefile 的目录,应该是docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM