打包 python 程式,變成一個可執行檔案
阿新 • • 發佈:2021-10-29
1. 安裝 pyinstaller
pip3 install pyinstaller
2. 改裝 matplotlib 3.1.1
現在的 matplotlib 版本都高於 3.2,而打包工具 pyinstaller需要低於 3.2 的版本,所以需要解除安裝 matplotlib,然後再安裝低版本
pip3 uninstall matplotlib
pip3 install matplotlib==3.1.1
這個命令好像是從官網(外網)下載地址下載 matplotlib3.1.1,所以下載巨慢,得等好幾分鐘。
3. 安裝 zeromq
實際操作中,總是報錯:沒有 zmq.h 這個檔案。網上查了一下,如下安裝:
npm install zeromq
我之前裝過 npm,所以這一步成功了。如果沒有裝過 npm,可能還得裝一下。我已經不記得 npm 是做什麼的了,這一步只是照著網上的攻略打的。
4. 打包!
首先打包一次
pyinstaller -F xxx.py
然後修改配置檔案 xxx.spec
vi xxx.spec
將 hiddenimports=[]
改為hiddenimports=['matplotlib']
。
再打包 xxx.spec 檔案:
pyinstaller -F xxx.spec
會自動生成 build, dist 資料夾,dist/xxx 即可執行檔案。
5. 參考
https://blog.csdn.net/kobeyu652453/article/details/108871179
https://blog.csdn.net/u012329294/article/details/83019889