問題:cmd窗口運行python腳本,報錯
C:\Users\xxx\Documents\GitHub\python3\main>python run_test.py
Traceback (most recent call last):
File "run_test.py", line 9, in <module>
from util.runmethod import RunMethod
ModuleNotFoundError: No module named 'util'
原因:在pycharm編輯器運行時,會將當前工程的所有文件夾路徑都作為包的搜索路徑;而在命令行中運行時,只是搜索當前路徑
解決方案:在run_test.py文件最前面加上以下代碼
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath)
