I got an ModuleNotFoundError
error as seen below:
sample % python3 sample1.py
Traceback (most recent call last):
File "sample1.py", line 4, in <module>
from util_lib.helper1 import helper1_print
ModuleNotFoundError: No module named 'util_lib'
sample %
.../Testing/sample/:
sample1.py (calls helper1.helper1_print())
.../Testing/util_lib/:
helper1.py (has helper1_print())
Codes
sample1.py:
import sys
sys.path.insert(0, '/Absolute/Path/Testing/util_lib')
from util_lib.helper1 import helper1_print
helper1_print()
**util_lab/helper1.py:**
def helper1_print():
print("Helper 1 Print")
Am I missing something?
I still have error when I used relative path instead of absolute path, see below:
sample1.py
import sys
sys.path.insert(0, '../Testing')
from util_lib.helper1 import helper1_print
helper1_print()
Error:
sample % python sample1.py
Traceback (most recent call last):
File "sample1.py", line 4, in <module>
from util_lib.helper1 import helper1_print
ModuleNotFoundError: No module named 'util_lib'
sample %
CodePudding user response:
It seems there is an issue while you are inserting path
to sys.path
as I see sample
and util_lib
directories are subdirectories of Testing
not API_Testing
So instead of
sys.path.insert(0, '/Users/pluu/PycharmProjects/API_Testing/util_lib')
try
sys.path.append("abs/path/to/Testing")