在ide中執行python程序,都已經在默認的項目路徑中,所以直接執行是沒有問題的。
但是在cmd下執行程序,所在路徑是python的搜索路徑,如果涉及到import引用就會報類似ImportError: No module named xxx這樣的錯誤,解決方法:
在報錯的模塊中添加:
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) #獲取當前絕對路徑 rootPath = os.path.split(curPath)[0] #獲取當前目錄的上一級目錄路徑 sys.path.append(rootPath)