0

I'm trying to import a class from a file, but I can't find the right syntax in Visual Studio Code :i

My structure:

└── src
    ├── common
    │   ├── database.py
    │  
    └── models
        ├── admin
              ├──admin.py

In admin.py, I need to import a class called Database, which is located in database.py. My solution would be:

from src.common.database import Database

But then I get this error:

ModuleNotFoundError: No module named 'src'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Malthe Reipurth
  • 33
  • 1
  • 1
  • 4

1 Answers1

0

As mentioned by @Dave in https://stackoverflow.com/a/7506029/2053706, you can use relative references to other files on import.

What you're looking for is:

from ...src.common.database import Database

Each "dot" is one path back, except the first "dot" which is "this folder".

Hope that makes sense.

Ari
  • 745
  • 1
  • 7
  • 23