pythonpython-sphinxpython-packagingautodocautosummary

Sphinx Autosummary (Autodoc) partially imports modules


Sphinx autosummary/autodoc gives error for some of the modules, but not all.

My code is opensource: https://github.com/dream-faster/krisi

I get the following error:

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

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

It imports some of the modules (eg.: compare.py) but fails to import others (regardless of which subdirectory they are in).

The directory structure:

library_name 
│
└───src
│   │
│   └───library_name
│         └─  __init__.py
│         │
│         └───module_1.py
│         │    └─   __init__.py
│         │    └─   compare.py
│         │    └─   report.py
│         │   
│         └───module_2.py
│              └─   __init__.py
│              └─   evaluate.py
│              └─   metric.py
│         
└───docs
    └───source
         └─   conf.py

Solutions I have tried:

1. Specifying the path (although it finds the module partially)

I have tried all variations of appending the path to sys.path:

   current_dir = os.path.dirname(__file__)
   target_dir = os.path.abspath(os.path.join(current_dir, "../../src/project_name"))
   sys.path.insert(0, target_dir)
   sys.path.insert(0, os.path.abspath("../.."))
   sys.path.insert(0, os.path.abspath("../../src"))
   sys.path.insert(0, os.path.abspath("../../src/project_name"))
   for x in os.walk("../../src"):
       sys.path.append(x[0])

2. Checking if all dependencies are installed.

I did a clean new conda environment and installed my package with pip install -e . All tests pass, that cover all modules.

3. Checking if cross module import is the culprit

Some modules reference other modules, eg.: module_1.metric references module_2.type However modules that were imported correctly do the same without an error.

What am I overlooking?


Solution

  • There were two problems I found, I am unsure which caused the problem. It was probably a combination of both and regardless of trying to be rigurous I didn't get the combination right. I went through methodically deleting all files and adding them back one by one.

    1. config file is now:

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

    1. No relative imports in any module

    I hope this helps someone else.