Search code examples
pythonpython-sphinxautodoc

Sphinx autodoc can't find module


I am new to sphinx and need help to figure out why I am getting the following error:

WARNING: autodoc: failed to import module 'employe_dao' from module 'models'; the following exception was raised:
No module named 'models'

My project structure is:

|--master_project
   |--sub_project
      |--docs
        |--build
        |--conf.py
        |--index.rst
        |--Makefile
        |--models.rst
        |--src.models.rst
        |--src.rst
      |--src
        |--models
          |--employee.py
          ...
        |--__init__.py
        |--data_extractor.py
        |--optimiser.py
    enter code here
        ...

This is a snipet from index.rst

...

.. toctree::
   :maxdepth: 2
   :caption: Contents:

.. automodule::src
   :members:
   :undoc-members:
   :show-inheritance:

.. automodule::models
   :members:
   :undoc-members:
   :show-inheritance:

...

* :ref:`modindex`

I have added sys.path.insert(0, os.path.abspath('./sub_project')) and uncomment import os, import sys in conf.py as recommened in Sphinx: autodoc can`t import module

sphinx-build fail - autodoc can't import/find module @ryandillan suggestion to add sys.path.insert(0, os.path.abspath('..')) to config.py fixed my 404 "index not found" error for model index for index.rst

I have added extensions = ['sphinx.ext.autodoc'] to config.py as recommend in another stackoverflow thread.

Any suggestions on what else I am doing incorrectly?


Solution

  • Based on your directory structure, the directory to add to sys.path should be ../src

    For a more general answer, think about what directory you would need to be in if you wanted to import module successfully in the Python CLI. That directory is the one you want Sphinx to have in your path.