我收到一個ModuleNotFoundError錯誤,如下所示:
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())
代碼
樣品1.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")
我錯過了什么嗎?
當我使用相對路徑而不是絕對路徑時,我仍然有錯誤,見下文:
樣本1.py
import sys
sys.path.insert(0, '../Testing')
from util_lib.helper1 import helper1_print
helper1_print()
錯誤:
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 %
uj5u.com熱心網友回復:
正如我所見,您在插入path時似乎存在問題,并且目錄是not的子目錄sys.pathsampleutil_libTestingAPI_Testing
所以而不是
sys.path.insert(0, '/Users/pluu/PycharmProjects/API_Testing/util_lib')
嘗試
sys.path.append("abs/path/to/Testing")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442842.html
標籤:python-3.x
