sphinx icon indicating copy to clipboard operation
sphinx copied to clipboard

autodoc failed to import module even though module is in path

Open sam-hoffman opened this issue 3 years ago • 4 comments

Describe the bug My project structure is like this:

.
├── service1
│   ├── README.md
│   ├── utils
│   │   ├── __init__.py
│   │   └── some_code.py
│   ├── routines
│   │   ├── __init__.py
│   │   ├── stuff
│   │   │   ├── __init__.py
│   │   │   └── more_code.py
│   │   └── control_class.py
├── service2
│   ├── README.md
│   ├── other_utils
│   │   ├── __init__.py
│   │   └── some_other_code.py
│   ├── routines
│   │   ├── __init__.py
│   │   ├── more_stuff
│   │   │   ├── __init__.py
│   │   │   └── even_more_code.py
│   │   └── control_class.py
├── docs
│   ├── Makefile
│   ├── build
│   ├── make.bat
│   └── source
│       ├── _static
│       ├── _templates
│       ├── service1
│       │   ├── utils.rst
│       │   ├── index.rst
│       │   ├── modules.rst
│       │   ├── routines.stuff.rst
│       │   └── routines.rst
│       ├── service2
│       │   ├── index.rst
│       │   ├── modules.rst
│       │   ├── routines.more_stuff.rst
│       │   └── routines.rst
│       ├── conf.py
│       └── index.rst

My conf.py has the following lines in it:

current_dir = os.path.dirname(__file__)
project = os.path.abspath(os.path.join(current_dir, "../../"))
sys.path.append(project)

service_paths = [
	os.path.abspath(os.path.join(current_dir, "../../service1")),
	os.path.abspath(os.path.join(current_dir, "../../service2")),
]
for p in service_paths:
	sys.path.append(p)

print(sys.path)

The top level project and all the modules seem to be in the sys.path, but I'm still getting warnings saying

WARNING: autodoc: failed to import module 'stuff.more_code' from module 'routines'; the following exception was raised:
No module named 'routines.stuff'

Any thoughts on how to resolve this?

sam-hoffman avatar Apr 29 '21 18:04 sam-hoffman

It seems both service1 and service2 have routines package. Is it possible to import it from python interpreter? Could you check it before using Sphinx?

tk0miya avatar Apr 30 '21 16:04 tk0miya

I am able to import it from the Python interpreter as long as I add service1 and service2 to sys.path.

I successfully ran the following from within the docs folder:

docs % ipython
Python 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 16:12:38) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: sys.path.append('/Users/shoffman/project/service1')

In [3]: sys.path.append('/Users/shoffman/project')

In [4]: sys.path.append('/Users/shoffman/project/service2')

In [5]: from routines.stuff import more_code

In [6]: from routines.stuff.more_code import MoreCodeClass

In [7]: 

sam-hoffman avatar Apr 30 '21 17:04 sam-hoffman

Could you share an example? I'd like to reproduce the error on my local.

tk0miya avatar May 01 '21 09:05 tk0miya

Did you solve your problem? I had the same problem.

Details0824 avatar Jul 21 '22 08:07 Details0824

I have the same problem, are there any solution guys?

rafa761 avatar May 18 '23 12:05 rafa761

The solution in general is:

  • Produce a MWE because the issue is generally at the RST level or the paths are incorrectly added (not in this case apparently). Also, be sure that the MWE does not have weird third party extensions (since they might be the culprit). Without a MWE, we cannot help.
  • Before submitting here, verify that you are running the latest Sphinx version (we don't do backports). Also, by experience, checking Stackoverflow before for related discussions or issues saves the day (and they might reply faster than us).

Since the OP hasn't produced a MWE, I will close the issue. For people having similar issues, check that your issue is also the same (autodoc may be tricky because one might think that the issue has the same cause but this is not always the case) and please first carefully check the points mentioned above and produce a MWE. Thank you.

EDIT: Feel free to reopen if you want to give a MWE btw.

picnixz avatar Aug 30 '23 08:08 picnixz