Modulenotfounderror: While Importing Module from Parent Folder

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

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

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.