問題:cmd窗口運行python腳本,報錯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'app
緣由:在pycharm編輯器運行時,會將當前工程的全部文件夾路徑都做爲包的搜索路徑;而在命令行中運行時,只是搜索當前路徑編輯器
解決方案:在run_test.py文件最前面加上如下代碼spa
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath)