python打包成exe,太大了該怎麼解決?
關鍵點就一個:要在虛擬環境裏安裝pyinstaller
如果你沒有在虛擬環境中安裝pyinstaller,你同樣可以使用pyinstaller命令,但是調用的是你係統原本的那個python編譯器,內含很多關聯庫,導致即使在虛擬環境中,你打包的exe文件仍然非常大。
另外一點要注意的是:要在虛擬環境裏安裝好你py文件中調用的庫,不然打包出來也沒法正常運行。
最後再複習一下正確流程 (前提是安裝好pipenv):
#建立虛擬環境
pipenv install
#進入虛擬環境(上一步可省略,因爲沒有虛擬環境的話會自動建立一個)
pipenv shell
#安裝模塊
pip install requests pyquery pysimplegui fake_useragent
#打包的模塊也要安裝
pip install pyinstaller
#開始打包
pyinstaller -Fw E:\test\url_crawler.py
警告(不影響程序生成)
WARNING: lib not found: api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python36\lib\site-packages\P
報錯是因爲打包時缺失了api-ms-win-crt-*-l1-1-0.dll這類型的數據,而它就存放在C:\Windows\System32\downlevel裏,因此加上 -p參數 添加該引用路徑就好了
解決辦法
PS C:\Users\Qiuyi> cd D:\Python_Projects\PyQt_Practices
PS D:\Python_Projects\PyQt_Practices> pyinstaller -p C:\Windows\System32\downlevel -F -w be_my_girlfriend.py
exe 生成位置
INFO: Appending archive to EXE D:\Python_Projects\PyQt_Practices\dist\be_my_girlfriend.exe
*.spec 是於pyinstaller生成的配置文件,下次打包時,可以不運行上面帶參數的命令,而直接運行:
pyinstaller transdocx.spec