安装 pyinstaller
要把Python脚本编译打包成.exe可执行文件,需要安装 pyinstaller.exe 。
打开windows控制台窗口cmd.exe,执行以下命令,Python会自动网络下载并安装。
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>cd C:\Program Files\Python37
C:\Program Files\Python37>python -m pip install pyinstaller
......略去N行......
Successfully installed altgraph-0.17 future-0.18.2 importlib-metadata-3.7.3 pefi le-2019.4.18 pyinstaller-4.2 pyinstaller-hooks-contrib-2021.1 pywin32-ctypes-0.2 .0 typing-extensions-3.7.4.3 zipp-3.4.1
安装完成后,pyinstaller.exe在scripts目录下:
C:\Program Files\Python37>scripts\pyinstaller usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--add-data <SRC;DEST or SRC:DEST>] [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR] [--hidden-import MODULENAME] [--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES] [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s] [--noupx] [--upx-exclude FILE] [-c] [-w] [-i <FILE.ico or FILE.exe,ID or FILE.icns or "NONE">] [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE] [--uac-admin] [--uac-uiaccess] [--win-private-assemblies] [--win-no-prefer-redirects] [--osx-bundle-identifier BUNDLE_IDENTIFIER] [--runtime-tmpdir PATH] [--bootloader-ignore-signals] [--distpath DIR] [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL] scriptname [scriptname ...] pyinstaller: error: the following arguments are required: scriptname
编译Python脚本
pyinstaller -F exam.py
生成的exam.exe在dist目录下,用Python官网首页的第一个示例来测试下:
# Python 3: Fibonacci series up to n def fib(n): a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() fib(1000) ''' 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 '''
编译过程会在源码目录下生成一个目录__pycache__,exam.exe在dist子目录下,文件大小5.6M,运行结果:
C:\Program Files\Python37\Scripts\dist>exam
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
编译时不带参数 -F ,将在 dist 目录下生成一个同名子目录,exam.exe在其中,还有很多其它文件,最主要的是 base_library.zip 和 python37.dll , 版本不同数字37也不同。
如用 tkinter 库生成一个简单的GUI窗口程序,文件更大,8.4M,源码及运行结果如下:
import tkinter win = tkinter.Tk() win.title('myWin') win.geometry("640x480") win.mainloop()
程序会带有一个控制台的黑窗口,编译时多用一个参数 -w 或 --noconsole 即可去掉。
1. pyinstaller -F -w exam.py 2. 或者: 3. pyinstaller -F exam.py --noconsole
生成的可执行文件有点大,参数 [--upx-dir UPX_DIR] 和 [--clean] 可用于压缩和清理。具体用法见:
https://blog.csdn.net/xinyingzai/article/details/80282856
Pyinstaller 打包发布经验总结:
https://blog.csdn.net/weixin_42052836/article/details/82315118
另外一个.py转.exe的工具: PY2EXE
py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.
官方网站:
下载地址:
https://pypi.org/project/py2exe/
py2exe仅适用于Windows平台。
附:upgrade pip
You are using pip version 10.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\Program Files\Python37>python -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/fe/ef/60d7ba03b5c442309ef4
2e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5MB)
37% |████████████▏ | 583kB 37kB/s eta 0:00:26
38% |████████████▍ | 593kB 37kB/s eta 0:00:26
39% |████████████▋ | 604kB 32kB/s eta 0:00:29
39% |████████████▊ | 614kB 22kB/s eta 0:00:41
。。。省去N行。。。
97% |███████████████████████████████▎| 1.5MB
98% |███████████████████████████████▌| 1.5MB
99% |███████████████████████████████▊| 1.5MB
99% |████████████████████████████████| 1.5MB
100% |████████████████████████████████| 1.5M
B 18kB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-21.0.1
C:\Program Files\Python37>