場景:使用pycharm編輯器啟動pyhon項目時可以啟動,但使用cmd啟動時,會報:no module named “xxx”的錯誤,此時,有兩種情況:
1.no module named “xxx”里的“xxx”是python的庫名;
此時說明引用庫沒成功,可能是因為電腦里有多個版本的python,
解決方法:在cmd中激活你所使用的python版本,例:activate python 3.6(如果安裝了python3.7,想使用3.6的環境請參考:https://www.cnblogs.com/xiaodai0/p/9780101.html)
2.no module named “xxx”里的“xxx”是本地的文件名;
此時說明系統沒有找到該文件夾
解決方法:在引入包的時候加上如下幾行代碼:
import sys current_dir = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(current_dir)[0] sys.path.append(rootPath)
如果想使用.bat文件自動執行,請參考:https://www.cnblogs.com/xiaodai0/p/10049635.html