python - import error from a sibling folder

Viewed 106

I'm working on a python 3.8.5 project which has this folder structure:

project/
    ├── __init__.py
    |── foo.py
    |── bar.py
    ├── utils/
    │   ├── __init__py
    │   └── configurator.py
    └── server/
        ├── __init__.py
        ├── models.py

In server/models.py I need to use a class Configurator declared inside utils/configurator.py:

# server/models.py
from utils.configurator import Configurator

# some code here ...

Unfortunately I get this error: ModuleNotFoundError: No module named 'utils'. I dont't understand how it cannot find the utils module since I've properly set the init.py file inside that folder.

I've also tried with relative import:

# server/models.py
from ..utils.configurator import Configurator
    
# some code here ...

this time getting the following error: ImportError: attempted relative import with no known parent package

Finally, I've attempted with an absolute import, with no results again:

# server/models.py
from projects.utils.configurator import Configurator
    
# some code here ...

The error this time is: ModuleNotFoundError: No module named 'projects'

Based on this previous question it seems correct to me, I can't understand what I'm messing with.

My code editor is Visual Studio Code and when I type from utils. ... it gives the right suggestion, although I don't think this piece of information could be of any utility.

0 Answers
Related