0

Let's say that I have a project with the directory structure as follows:

project
      \__ cfgs
             \__ __init__.py
             \__ config.py
      \__ src
            \__ __init__.py
            \__ model1
                     \__ __init__.py
                     \__ test.py
            \__ model2
                     \__ test2.py

I want to run the Python script test.py. However, I need to import a module named Config from the file project/cfgs/config.py. How would I correctly import it?

Within test.py I've tried the following:

# Returns a `No module named 'cfgs'` error.
from cfgs import Config

# Returns `Attempted relative import with no known parent package` error.
from ..cfgs import Config
Sean
  • 2,890
  • 8
  • 36
  • 78
  • 1
    You need to set your project up as a package first, see [answer](https://stackoverflow.com/a/28154841/). Once your project is set up as a proper package and put the files into the correct directories the problems with import issues will go away – metatoaster May 22 '23 at 05:19

0 Answers0