How to import a Python module from a sibling folder? How to import a Python module from a sibling folder? python python

How to import a Python module from a sibling folder?


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.


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 directorysys.path.append("..")from SiblingB import Bprint("B is successfully imported!")