How to import a Python module from a sibling folder?
Solution 1:
This is happening because A
and B
are independent, unrelated, packages as far as Python is concerned.
Create a __init__.py
in the same directory as Driver.py
and everything should work as expected.
Solution 2:
In my case adding __init__.py
was not enough. You also have to append the path of the parent directory if you get module not found error.
root :
|
|__SiblingA:
| \__A.py
|
|__SiblingB:
| \_ __init__.py
| \__B.py
|
To import B.py from A.py, you have to do the following
import sys
# append the path of the parent directory
sys.path.append("..")
from SiblingB import B
print("B is successfully imported!")