Skip to content Skip to sidebar Skip to footer

Python Modulenotfounderror Error In Absolute Import Package?

I have a file structure like this. . └── E:\test ├ ├── M2 │ └── demo.py | │ └── MA │ └── MA1

Solution 1:

The python interpreter does not search modules in test\ directory since you execute the script from test\M2\. You can tell python to search from test\ by adding directory "test" to the PYTHONPATH variable. For windows command line:

set PYTHONPATH=%PYTHONPATH%;E:\test

For linux terminal:

export PYTHONPATH=path_to_test_dir

Solution 2:

Remember 2 things:

1- Python searches sys.path to find modules and packages.

2- When you run your script like you did, only it's directory will be added to the sys.path. So you have to add the directory which MA is inside it manually. Do the following inside demo.py:

import sys
sys.path.insert(0, r'E:\test')

from MA.MA1.ma1 import foo
print(foo())

Alternatively you can set PYTHONPATH variable.

Post a Comment for "Python Modulenotfounderror Error In Absolute Import Package?"