0

There are lots of similar questions, but none of them seem to help me with this. I have the following file structure:

rest_api
    __init__.py
    utils
        __init__.py
        config.py
    foo
        __init__.py
        foo.py

I'd like to import the Config class from config.py, into foo.py. I attempt to run:

pipenv run python rest_api/foo/foo.py

I have tried numerous things, including adding the parent folder to PATH and using relative imports, but the import line always fails:

from rest_api.utils.config import Config

I get the following error:

ModuleNotFoundError: No module named 'rest_api'

I should mention that the __init__.py files are all empty. Not sure if they are neccessary, since I have Python 3.8.2.

Rafael Sanchez
  • 394
  • 7
  • 20
  • Can you please provide an example that reproduces this error? Please read up in absolute imports in Python3. Your issue is related to trying to use relative imports in Python3 which won’t work – Vimal Thilak May 03 '20 at 18:18
  • Does this answer your question? [Import from sibling directory](https://stackoverflow.com/questions/4542352/import-from-sibling-directory) – Tomerikoo May 03 '20 at 18:25
  • @Tomerikoo: I do have `__init__.py` files at each level in the file structure, as the accepted answer in the link suggests. I also have tried removing them and the issue stays the same – Rafael Sanchez May 03 '20 at 22:25
  • It would be helpful if you showed how you are actually trying to do this. For example, the `import` line in `foo.py` – Tomerikoo May 04 '20 at 05:38
  • I edited the question to show exactly how the import line looks like – Rafael Sanchez May 04 '20 at 09:21

0 Answers0