Modules are not importing into another file which is another folder, I tried to search for the possible solutions with no luck. Here is my practice file hierarchy level
python_practice (parent folder)
|
|--------Modules (sub_folder)
| |
| |-----fol_1
| | |
| | |---file_operations.py
| |
| |-----fol_2
| | |
| | |---add.py
|-----app.py
I am trying to import "file_operations.py" in "add.py" by using absolute imports from Modules.fol_1 import file_operations
but when I run the file I am getting error as ModuleNotFoundError: No module named 'Modules'
, this happens the same for add.py
file when I tried importing file_operations.py
into it through absolute importing.
Then I tried importing through relative imports by using the following syntax from .fol_1 import file_operations
but I am getting error as ImportError: attempted relative import with no known parent package
the same goes for add.py
when I try to import file_operations.py
. But when I import both add.py
and file_operations.py
in app.py
file they work fine and program runs successfully.
Note : I have installed python in "c" drive and I have python_practice
folder in "D" drive.
PS : I wrote Both "file_operations.py" & "add.py" as modules.
I came across several answers for this I tried this answer Python: import module from another directory at the same level in project hierarchy by using double dot relative imports but didn't work for me, getting same error as mentioned above. Then I tried by adding the python_practice
(parent folder) to path variables
in python even after trying that the same error is coming for both absolute and relative imports.