1

When I try to access a sub-directory module from another sub-directory module it keep shows:

ModuleNotFoundError: No module named 'folder_1'

My current folders structure looks like this:

.
├── main.py (updated)
├── folder_1
│   ├── file1.py
│   └── __init__.py
└── folder_2
    ├── file2.py
    └── __init__.py

From file1.py:

def abc():
   return True;

From folder_1/__init__.py

from . import file1

From folder2/file2.py:

from folder_1.file1 import abc

Is this the wrong way to access sub-folder module from another sub-folder?

  • 3
    I guess you need to check the documentation about [The Module Search Path](https://docs.python.org/3/tutorial/modules.html#the-module-search-path) and [Packages](https://docs.python.org/3/tutorial/modules.html#packages). – accdias Feb 20 '20 at 02:17
  • I came across this, is it not possible to import file1.py from file2.py and run file2.py? I tried adding a main.py at the root and run file2.py it works as mentioned in the link. https://stackoverflow.com/questions/58076630/python-import-scrypt-from-subfolder-to-another-subfolder – rosepalette Feb 20 '20 at 03:33
  • Python (and possibly your IDE) needs to know that the folder that has `main.py` in it is a source root, so that it can find the `folder_1` module there. – Grismar Feb 20 '20 at 03:38

0 Answers0