0

I have the following project structure and I would like to import the function "get()", which is inside the main.py from the Folder_A, into the model.py file from Folder_BA.

Project
     |_____ Folder_A
     |          |_____ main.py
     |          |_____ model.py
     |          |_____ constants.py
     |          |_____ functions.py
     |
     |_____ Folder_B
                |_____ Folder_BA
                |         |_____ main.py
                |         |_____ model.py
                |         |_____ functions.py
                |
                |_____ Folder_BB
                          |_____ main.py
                          |_____ model.py
                          |_____ functions.py

 

I tried coding inside the model.py file from Folder BA the following code but it didnt work:

from Folder_A.main import get

ModuleNotFoundError: No module named 'Folder_A'

Thank you.

Nacho Eigu
  • 21
  • 4

1 Answers1

0

From this answer:

By default, you can't. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).

So a few things you can do:

  1. Try structuring your files into packages, adding a __init__.py empty file inside the folder so it can be imported to other files.

  2. You can add a new path using sys and then just use regular import

sys.path.append('/path/to/Project/Folder_A')
from main import get
Rodrigo Cava
  • 173
  • 9