0

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.

sudheer68
  • 1
  • 2
  • 1
    Do note that Python imports _do not navigate directories_. Python imports are only resolved by searching the Python path. `from .X` doesn't mean "look for X in the same folder", it means "append `.X` to the current package name, and them import the module corresponding to that fully qualified dotted name". – Brian61354270 Jul 29 '23 at 12:12

0 Answers0