Jonas Meddeb
Jonas Meddeb

Reputation: 1

Why is the relative import not working in my project?

I am working on a python project which consist of several packages and modules. Hence I need to import modules from different packages. However, when importing these modules I am getting import errors. The folder structure is as follows: Image of folder structure

Within the module Stage0.py I use the relative import: "from ..data.Datapipe import DataFactory" to import the class DataFactory. However when I execute the script I get an error message: "ModuleNotFoundError: No module named 'MT'"

I would appreciate any feedback as I'm becoming desperate

Upvotes: 0

Views: 1437

Answers (2)

Ryan Jones
Ryan Jones

Reputation: 21

I'm not 100% sure I've understood this correctly, but by the looks of it when you run the Stage0.py it will set the root python path to the app directory, the problem is that python's use of relative imports only allows to find files within that root directory, this site explains it a bit better to be honest

See here

Note that for relative imports, the dots . can go up only up to (but not including) the directory containing the script run from the command line

Anyway, it to fix it you could create a __main__.py file within the mt folder which calls the Stage0.py script, this will set that mt folder as the root of the project giving access to the other directories.

Upvotes: 1

fraco_oxza
fraco_oxza

Reputation: 66

As a simple solution, I recommend that you put Stage0.py in the same location as the requirements.txt and execute it from there

as follows:

-data/
    -__init__.py
    -Datapipe.py
-graph/
    -__init__.py
    -s0.py
-stage0.py

stage0.py

from data.Datapipe import DataFactory

Upvotes: 0

Related Questions