python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > PyInstaller打包Python脚本为.exe

PyInstaller将Python脚本打包为.exe可执行文件的步骤详解

作者:Lorin洛林

Python是一门强大而灵活的编程语言,为了方便共享和部署,我们可以将 Python 脚本打包为可执行文件(.exe),这样其他用户就无需安装 Python环境,直接运行可执行文件即可,本文将介绍如何使用PyInstaller 工具实现这一目标,需要的朋友可以参考下

前言

PyInstaller

特点

跨平台支持

自动依赖项处理

单文件发布

支持图形用户界面(GUI)和命令行界面(CLI)应用

支持多种打包选项

基本用法

pyinstaller your_script.py

常用参数

pyinstaller --onefile your_script.py
pyinstaller --name your_custom_name your_script.py
pyinstaller --onefile --icon=your_icon.ico your_script.py
pyinstaller --onefile --requirements=requirements.txt your_script.py
pyinstaller --onefile --requirements=requirements.txt your_script.py

其它参数

--clean: 在打包前删除临时文件和输出目录。
--debug: 在调试模式下运行 PyInstaller。
--log-level: 指定日志级别,如 --log-level=DEBUG。
-w 是一个命令行选项,用于生成无控制台窗口的可执行文件。
-p 用于添加模块搜索路径。

版本 & 环境

实现步骤

安装 PyInstaller

pip install pyinstaller
pip install pyinstaller==3.6.0

创建 Python 脚本

使用 PyInstaller 打包

$ pyinstaller --onefile -p venv/Lib/site-packages 20231207.py
39 INFO: PyInstaller: 3.6
39 INFO: Python: 2.7.18
39 INFO: Platform: Windows-10-10.0.22621
39 INFO: wrote C:\Users\MQiuj\PycharmProjects\pythonProject\20231207.spec
40 INFO: UPX is not available.
45 INFO: Extending PYTHONPATH with paths
['C:\\Users\\MQiuj\\PycharmProjects\\pythonProject',
 'C:\\Users\\MQiuj\\PycharmProjects\\pythonProject\\venv\\Lib\\site-packages',
 'C:\\Users\\MQiuj\\PycharmProjects\\pythonProject']
45 INFO: checking Analysis
46 INFO: Building because pathex changed
46 INFO: Initializing module dependency graph...
46 INFO: Caching module graph hooks...
53 INFO: Caching module dependency graph...
61 INFO: running Analysis Analysis-00.toc
62 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
  required by c:\python27\python.exe
115 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_acd031d7e1db7c28.manifest
167 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.9635_none ...
167 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_08e2c157a83ed5da.manifest
168 INFO: Searching for file msvcr90.dll
168 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_08e2c157a83ed5da\msvcr90.dll
168 INFO: Searching for file msvcp90.dll
168 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_08e2c157a83ed5da\msvcp90.dll
168 INFO: Searching for file msvcm90.dll
168 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_08e2c157a83ed5da\msvcm90.dll
219 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9635_none_acd031d7e1db7c28.manifest
219 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9635)
240 INFO: Analyzing C:\Users\MQiuj\PycharmProjects\pythonProject\20231207.py
951 INFO: Processing pre-safe import module hook   urllib3.packages.six.moves
2621 INFO: Processing module hooks...
2621 INFO: Loading module hook "hook-httplib.py"...
2622 INFO: Loading module hook "hook-encodings.py"...
2865 INFO: Loading module hook "hook-certifi.py"...
2874 INFO: Looking for ctypes DLLs
2874 INFO: Analyzing run-time hooks ...
2875 INFO: Including run-time hook 'pyi_rth_certifi.py'
2877 INFO: Looking for dynamic libraries
2930 INFO: Looking for eggs
2930 INFO: Using Python library C:\WINDOWS\system32\python27.dll
2930 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'amd64', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 9635), publicKeyToken=u'1fc8b3b9a1e18e3b')]
2933 INFO: Warnings written to C:\Users\MQiuj\PycharmProjects\pythonProject\build\20231207\warn-20231207.txt
2951 INFO: Graph cross-reference written to C:\Users\MQiuj\PycharmProjects\pythonProject\build\20231207\xref-20231207.html
2982 INFO: checking PYZ
2983 INFO: Building because toc changed
2983 INFO: Building PYZ (ZlibArchive) C:\Users\MQiuj\PycharmProjects\pythonProject\build\20231207\PYZ-00.pyz
3257 INFO: Building PYZ (ZlibArchive) C:\Users\MQiuj\PycharmProjects\pythonProject\build\20231207\PYZ-00.pyz completed successfully.
3288 INFO: checking PKG
3288 INFO: Building because toc changed
3288 INFO: Building PKG (CArchive) PKG-00.pkg
3384 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9635)
4298 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
4299 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
4299 INFO: checking EXE
4300 INFO: Building because toc changed
4300 INFO: Building EXE from EXE-00.toc
4300 INFO: Appending archive to EXE C:\Users\MQiuj\PycharmProjects\pythonProject\dist\20231207.exe
4306 INFO: Building EXE from EXE-00.toc completed successfully.

运行

总结

以上就是PyInstaller将Python脚本打包为.exe可执行文件的步骤详解的详细内容,更多关于PyInstaller Python脚本打包为.exe的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文