pythonpython-sphinxautodoc

Sphinx make html fails: WARNING: autodoc: failed to import module


I have an error with Sphinx Autodoc.

I am not sure why it can't find those modules.

My project structure is:

    Project
    |
    |--Main Folder
    |    |--docs
    |        _build
    |        _static
    |        _templates
    |        conf.py
    |        index.rst
    |        make.bat
    |        Makefile
    |        modules.rst
    |        program.rst
    |    |--program
    |          __init__.py
    |          Camera.py
    |          Database.py
    |          Dataset.py
    |          Detection.py
    |          ...

conf.py

import os
import sys

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


extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.napoleon'
]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

html_theme = 'sphinx_rtd_theme'
html_static_path = []

modules.rst

program
=======

.. toctree::
   :maxdepth: 4

   program

Detection.py Just the first lines from the import

from Camera import Camera

The Error

The weird thing is that Sphinx can create the documentation for the Camera file with easy, but it fails with the following error:

WARNING: autodoc: failed to import module 'Detection' from module 'program'; the following exception was raised: No module named 'Camera'

So this is because in Detection.py I import my Camera.py.

Does someone see my error? Because in the program, the imports work fine and I can access all the classes.


Solution

  • try this:

    from program.Camera import Camera
    

    or

    from .Camera import Camera