安装pyinstaller
pip install pyinstaller
或者
python -m pip install pyinstaller
使用pyinstaller
pyinstaller -F helloworld.py
其中,-F 表示打包成单独的 .exe 文件,这时生成的 .exe 文件会比较大,而且运行速度回较慢。仅仅一个 helloworld 程序,生成的文件就 5MB 大。
另外,使用 -i 还可以指定可执行文件的图标;
-w 表示去掉控制台窗口,这在 GUI 界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!
PyInstaller 会对脚本进行解析,并做出如下动作:
1、在脚本目录生成 helloworld.spec 文件;
2、创建一个 build 目录;
3、写入一些日志文件和中间流程文件到 build 目录;
4、创建 dist 目录;
5、生成可执行文件到 dist 目录;
执行流程:
PS C:\Users\5800h-02> pyinstaller -F C:\Users\5800h-02\Desktop\test.py
118 INFO: PyInstaller: 5.12.0
118 INFO: Python: 3.8.8
119 INFO: Platform: Windows-10-10.0.22621-SP0
119 INFO: wrote C:\Users\5800h-02\test.spec
120 INFO: UPX is not available.
122 INFO: Extending PYTHONPATH with paths
['C:\\Users\\5800h-02\\Desktop']
337 INFO: checking Analysis
338 INFO: Building Analysis because Analysis-00.toc is non existent
338 INFO: Initializing module dependency graph...
339 INFO: Caching module graph hooks...
362 INFO: Analyzing base_library.zip ...
1186 INFO: Loading module hook 'hook-heapq.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
1308 INFO: Loading module hook 'hook-encodings.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
2069 INFO: Loading module hook 'hook-pickle.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
2816 INFO: Caching module dependency graph...
2907 INFO: running Analysis Analysis-00.toc
2928 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\5800h-02\appdata\local\programs\python\python38\python.exe
3107 INFO: Analyzing C:\Users\5800h-02\Desktop\test.py
3690 INFO: Loading module hook 'hook-platform.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
3880 INFO: Loading module hook 'hook-difflib.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
3993 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
4150 INFO: Processing module hooks...
4153 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\hooks'...
4154 INFO: checking Tree
4154 INFO: Building Tree because Tree-00.toc is non existent
4993 INFO: Using Python library c:\users\5800h-02\appdata\local\programs\python\python38\python38.dll
4993 INFO: Found binding redirects:
[]
4996 INFO: Warnings written to C:\Users\5800h-02\build\test\warn-test.txt
5014 INFO: Graph cross-reference written to C:\Users\5800h-02\build\test\xref-test.html
5047 INFO: checking PYZ
5047 INFO: Building PYZ because PYZ-00.toc is non existent
5047 INFO: Building PYZ (ZlibArchive) C:\Users\5800h-02\build\test\PYZ-00.pyz
5311 INFO: Building PYZ (ZlibArchive) C:\Users\5800h-02\build\test\PYZ-00.pyz completed successfully.
5329 INFO: checking PKG
5329 INFO: Building PKG because PKG-00.toc is non existent
5329 INFO: Building PKG (CArchive) test.pkg
7257 INFO: Building PKG (CArchive) test.pkg completed successfully.
7272 INFO: Bootloader c:\users\5800h-02\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
7272 INFO: checking EXE
7274 INFO: Building EXE because EXE-00.toc is non existent
7274 INFO: Building EXE from EXE-00.toc
7274 INFO: Copying bootloader EXE to C:\Users\5800h-02\dist\test.exe.notanexecutable
7444 INFO: Copying icon to EXE
7445 INFO: Copying icons from ['c:\\users\\5800h-02\\appdata\\local\\programs\\python\\python38\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
7445 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
7446 INFO: Writing RT_ICON 1 resource with 3752 bytes
7446 INFO: Writing RT_ICON 2 resource with 2216 bytes
7446 INFO: Writing RT_ICON 3 resource with 1384 bytes
7446 INFO: Writing RT_ICON 4 resource with 37019 bytes
7446 INFO: Writing RT_ICON 5 resource with 9640 bytes
7446 INFO: Writing RT_ICON 6 resource with 4264 bytes
7446 INFO: Writing RT_ICON 7 resource with 1128 bytes
7619 INFO: Copying 0 resources to EXE
7620 INFO: Embedding manifest in EXE
7620 INFO: Updating manifest in C:\Users\5800h-02\dist\test.exe.notanexecutable
7621 INFO: Updating resource type 24 name 1 language 0
7790 INFO: Appending PKG archive to EXE
7796 INFO: Fixing EXE headers
7878 INFO: Building EXE from EXE-00.toc completed successfully.
生成文件:dist文件夹。里面包含text.exe文件