Dima  Sakovich Dima Sakovich - 27 days ago 17
Python Question

Sphinx autodoc in Flask ImportError: no module named

I'm trying to set Sphinx autodoc for my Flask project.
Here is schema of my project:

Folder
|--docs
| |--build
| |--source
| conf.py
| index.rst
| mod_ololo.rst
| ....
|--flask_app
| |--celery_worker.py
| |--config.py
| |--run.py
| |--app
| |--__init__.py
| |--temp.py
| |--mod_search
| ....
| |--mod_files
| |--__init__.py
| ....


and so on.

In conf.py I already set:

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


I'm trying to add content of temp.py to my documentation.
Here is content of mod_ololo.rst:

*******************************************
OLOLO module
*******************************************

Ololololo
======

.. automodule:: flask_app.app.temp
:members:


And when I run
make html
for separate scripts - Sphinx autodoc works, but in case of my flask project it doesn't and shows me the following error:

ImportError: No module named 'config'


Where config is config.py

Answer

Purely a guess, but the config module that your sphinx builder isn't finding is probably the config.py module in your flask_app folder. There's likely an import config statement somewhere in your flask_app/app/temp.py file, so when sphinx tries to run the automodule procedure on it, it gets a failure on that import.

From your directory structure it looks like your flask_app folder is not a python package, so you might have to add that folder rather than (or in addition to) its parent folder to the path in your conf.py file:

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

Also, you'll likely have to change your automodule statement in mod_ololo.rst not to reference flask_app as a package:

.. automodule:: app.temp

If you actually do intend to use flask_app as a package, then you need an __init__.py file in that folder, and your import statements within your project should be absolute from the top flask_app package, i.e.:

from flask_app import config

rather than

import config