1

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?

mzjn
  • 48,958
  • 13
  • 128
  • 248
semyd
  • 430
  • 4
  • 17
  • In which context do you get the import errors ? When you build the doc ? Using which command (in what directory) ? It may be a PATH issue. Also, you are using Conda exclusively for managing your environment ? – Lenormju Jan 10 '23 at 06:45
  • I noticed a discrepancy in your directory structure (`library_name`) and your sys.path (`project_name`). However it looks like you found and fixed the issue in https://github.com/dream-faster/krisi/commit/9cfcdbb69e660f83c952c35cfd0b1fb7aa2daf08 – Steve Piercy Jan 10 '23 at 08:35
  • Thanks both for looking at it! - I'm getting import errors both locally by running ``make html`` in the docs folder and on github workflow - I'm using a conda alternative (mamba), which is nearly identical. I install the libary with ``pip install -e .`` – semyd Jan 10 '23 at 16:50
  • Yes, I mistakenly wrote the wrong code before but corrected it – semyd Jan 10 '23 at 16:51

1 Answers1

1

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.

semyd
  • 430
  • 4
  • 17
  • The proper solution would be using Python virtual environments where both your project and Sphinx are installed in the same virtual environment. There should never be a need for a normal Python dev to poke `sys.path`. – Mikko Ohtamaa Jan 19 '23 at 14:29
  • Both my project and Sphinx are installed in the same python environment. However ``conf.py`` is two subdirectories down, hence can't find the project directory. – semyd Jan 19 '23 at 14:40