Python 程序打包为exe可执行文件
一、简述
记--将python程序打包为exe可执行程序,以便在其它没有安装python环境的电脑运行。
二、安装pyinstaller
打开cmd命令窗口,到python安装路径,输入pip.exe install pyinstaller

安装成功后执行:pyinstaller.exe -v查看版本信息。
如果不知道python安装路径,可以使用以下方式查看:


三、打包为exe可执行程序
打包命令:pyinstaller -F test.py
(-F参数指定生成独立的可执行文件,简单来说是一个exe,不加该参数就有动态库)
其中test.py是测试程序:
with open("test.txt", "w") as testFile:
testFile.write("pack python to exe.\r\n")
print("finish!\r\n")
input("请按任意健继续...");

打包成功:

运行exe效果:

附
1、如果提示找不到pip.exe, pyinstaller.exe
请使用绝对路径或将exe所在路径添加到环境变量中。
比如例子中将C:\Users\Liang\AppData\Local\Programs\Python\Python38\Scripts\添加到环境变量。
2、离线安装pyinstaller
下载并安装各种依赖库: 解压后到解压目录执行cmd命令:python setup.py install
2.1 https://www.cnpython.com/pypi/pywin32-ctypes/downloadpywin32-ctypes:https://www.cnpython.com/pypi/pywin32-ctypes/download (exe就直接安装)
2.2 future:https://pypi.org/simple/future/
2.3 pefile:https://pypi.org/simple/pefile/
2.4 altgraph:https://pypi.org/simple/altgraph/
下载pyinstaller包:Downloads — PyInstaller bundles Python applicationshttp://www.pyinstaller.org/downloads.htmlDownloads — PyInstaller bundles Python applications
解压后到解压目录执行cmd命令:python setup.py install

本文介绍了如何将Python程序打包成exe可执行文件,以便在没有Python环境的计算机上运行。首先,通过pip安装PyInstaller,然后使用命令`pyinstaller -F test.py`进行打包,其中test.py是一个简单的写入文件并打印信息的程序。打包成功后,运行exe文件可验证程序执行效果。注意,如果遇到找不到pip或pyinstaller的问题,可能需要添加环境变量或使用完整路径。此外,还提供了离线安装PyInstaller的步骤和相关依赖库的下载链接。
1529

被折叠的 条评论
为什么被折叠?



