4

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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Adam
  • 473
  • 5
  • 21

1 Answers1

5

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.

Ammar Askar
  • 475
  • 3
  • 9
  • Getting this error now `WARNING: autodoc: failed to import module 'optimiser' from module 'src'; the following exception was raised: No module named 'optimiser_engine'`. Any ideas what is causing it? – Adam Sep 03 '19 at 04:17
  • Is `optimiser_engine.py` under the src folder? If so, the right way to tell autodoc how to find it would be `.. automodule::optimiser_engine` – Ammar Askar Sep 03 '19 at 04:20
  • I used to have a file by that named, but refactored it to `optimiser.py` and there is not anymore. I have check src.rst and it does not contain reference to `optimiser_engine` – Adam Sep 03 '19 at 04:26
  • 2
    Aah whoops, I misread the error a little. Is `optimiser.py` trying to import `optimiser_engine` or something? Try this too, go into your `src` folder, open up `python` in the command line and do a `import optimiser`, see if it throws an error. – Ammar Askar Sep 03 '19 at 04:28
  • You are correct. It throws error `ModuleNotFoundError: No module named 'optimiser_engine'` and references line to library import. Fixed this by adding `'sys.path.insert(0, os.path.abspath('../..'))'`. Should I be including the 3rd library imports? It is also doing for the `mysql` and `mip` library`. – Adam Sep 03 '19 at 04:40
  • 1
    Hmm, are your 3rd party libraries installed at the system level, say with pip? If so, you shouldn't need anything special, they should just work. Make sure you're not forgetting to activate a virtualenv if you have one of those, and again try doing an `import mysql` in the python shell. If your libraries are installed in some funky different way, then you might need to add them to the path. – Ammar Askar Sep 03 '19 at 04:43
  • Ahh, this is the issue. I had been running a virtualenv and migrated to conda env, re-install conda mysql-connector and it fixed the problem – Adam Sep 03 '19 at 04:54