import a file from different folder in python

Viewed 71

I am trying to import a .py from a different folder in my project but got the Module not found error. Please help.

this is my project tree.

PROJECT_name
+trnsvr
|
\--trn_db.py

+websvr
|
\--instance
   |
   --config.py
\--app
   |
   --__init__.py

in trn_db.py I did import websvr.instance.config as conf

but got the ModuleNotFoundError as follow.

ModuleNotFoundError: No module named 'websvr'

how do I resolve this? thank you

2 Answers

Try copying this into trn_db.py. I got this from here.

import sys
sys.path.insert(1, '../websvr/instance/')
import config as conf

Try to specify the file path... c:/MicrosoftVSCode/randomthings1.py. When not right next to it (in the same folder), python has sometimes problems accessing files. Also, make sure antimalware software isn't blocking python's access. <- My case

Related