3

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.

dc_Bita98
  • 851
  • 2
  • 17
  • 35
  • I have the exact same problem. Did you manage to find a work around it? – Italo Sep 08 '21 at 20:49
  • [look at this post](https://stackoverflow.com/questions/26384041/python-import-fails-in-windows-but-not-linux) add this lines at the beginning of each python file ``` import os, sys if __name__ == '__main__': sys.path.append(os.getcwd()) sys.path[0]=os.path.dirname(os.path.realpath(__file__)) ``` – dc_Bita98 Sep 08 '21 at 21:22

0 Answers0