PyInstaller
PyInstaller
可以將 python script 打包成可執行文件。
安裝
pip install pyinstaller
使用
pyinstaller
文檔 : https://pyinstaller.readthedocs.io/en/stable/usage.html
打包成單個文件
pyinstaller -F test.py
解決vcruntime140.dll沒有被指定在Windows上運行(1)
關閉upx即可
pyinstaller -F --noupx test.py
解決vcruntime140.dll沒有被指定在Windows上運行(2)
開啓upx
upx下載
: https://github.com/upx/upx/releases ,將upx.exe添加到環境變量
linux/WSL
: sudo apt install upx
upx簡單使用
Usage: upx [-123456789dlthVL] [-qvfk] [-o file] file..
upx -5 -k test.exe #壓縮級別:5 ,將原始文件備份
upx --lzma sha512sum.exe #pyinstaller默認壓縮方式
問題分析
pyinstaller -F test.py #打包
$tree
.
├── __pycache__
│ └── test.cpython-37.pyc
├── build
│ └── test
│ ├── Analysis-00.toc
│ ├── EXE-00.toc
│ ├── PKG-00.pkg
│ ├── PKG-00.toc
│ ├── PYZ-00.pyz
│ ├── PYZ-00.toc
│ ├── base_library.zip
│ ├── test.exe.manifest
│ ├── warn-test.txt
│ └── xref-test.html
├── dist
│ └── test.exe
├── test.py
└── test.spec
4 directories, 14 files
dist/test.exe #打包完的文件
編輯器打開build/test/Analysis-00.toc
,找到VCRUNTIME140.dll
所在行,('VCRUNTIME140.dll', 'd:\\anaconda3\\VCRUNTIME140.dll', 'BINARY')
,問題出在VCRUNTIME140.dll
的版本。
將D:\Anaconda3\vcruntime140.dll
重命名爲vcruntime140.dll.bak
將C:\Windows\System32\vcruntime140.dll
複製到D:\Anaconda3
D:\Anaconda3
爲Anaconda python3
的安裝路徑。
查看C:\Windows\System32\vcruntime140.dll
的屬性\詳細信息
,版本號爲14.21.27702.2
。在文件地址輸入控制面板\程序\程序和功能
,找到了vcruntime140.dll
的安裝程序VC++ 2015-2019 v14.21.27702.2
。
若vcruntime140.dll
的版本版本比較低,下載VC++ 2015-2019,具體原因:Visual C++ 2015、2017 和 2019 都共享相同的可再發行軟件文件,之前的版本使用獨立runtime
。
將打包好文件傳到win7虛擬機運行不了:同樣安裝VC++ 2015-2019
。