ModuleNotFoundError: While importing module from parent folder

I'm using python 3.6.X.

I have following tree structure:

BS/ BS.py test_BS.py SI/ SI.py 

I wish to import BS.py inside SI.py hence wrote:

import BS

but it's giving the error message:

File "SI.py", line 1, in <module>
import BS
ModuleNotFoundError: No module named 'BS'
1

2 Answers

import os, sys
#Following lines are for assigning parent directory dynamically.
dir_path = os.path.dirname(os.path.realpath(__file__))
parent_dir_path = os.path.abspath(os.path.join(dir_path, os.pardir))
sys.path.insert(0, parent_dir_path)
import BS
1

You need to add your dir to the PYTHONPATH:

export PYTHONPATH="${PYTHONPATH}:/your/dir/BS"
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like