打包python檔案成exe可執行檔案
阿新 • • 發佈:2022-05-30
安裝pyinstaller
- 專門打包python檔案的:cmd中安裝
pip3 install pyinstaller
開始打包
- 需要一個圖示檔案(ico)和要打包的py檔案:需要注意的是,生產的main.exe檔案要通過everthing查詢
pyinstaller -F -w -i D:\projectFiles\pythonFiles\pyautogui\xiayu.ico D:\projectFiles\pythonFiles\pyautogui\下雨通知\main.py
常見報錯
沒有對應的模組:ModuleNotFoundError: No module named 'xxx'
- 說明:
這個錯誤是因為pyinstaller沒有把py檔案import的包打包進去導致的,其實pyinstaller打包的時候會生產一個spec檔案,可以去裡面配置要打包的內容。 - 解決辦法:
- 首先還是執行一次打包命令:
pyinstaller -F -w -i D:\projectFiles\pythonFiles\pyautogui\xiayu.ico D:\projectFiles\pythonFiles\pyautogui\下雨通知\main.py
- 然後通過everthing查詢打包main.py生成的main.spec(相當於打包的配置檔案)
- 找到後用記事本開啟:這個pathex引數就是依賴包地址(為空的話就表示沒有打包依賴包,就要加入地址)
pathex=['D:\\projectFiles\\pythonFiles\\pyautogui\\venv\\Lib\\site-packages'],
- 怎麼找這個以來地址呢:比如我們程式中import了xlrd
- 通過spec檔案進行打包:不再通過上面的打包命令,直接打包spec檔案即可
pyinstaller D:\projectFiles\pythonFiles\pyautogui\下雨通知\main.spec