文号
943399
阅读
6670
回复
3
收藏
3
如何将python3.6软件的py文件打包成exe程序(教程)
2025/04/19
原创
IP:山西
摘要:The article mainly discusses two packaging tools: cx_Freeze and pyinstaller. ‌cx_Freeze‌: This method is suitable for Python version 3.3. However, for newer Python versions (such as 3.6.2), updated to a compatible version. The article mentions issues encountered when attempting to use cx_Freeze, such as not finding the cxFreeze.bat file, and solving the problem by running the python cxfreeze-postinstall command. ‌pyinstaller‌: Due to the limitations of cx_Freeze, the article turns to pyinstaller as the packaging tool. It provides detailed instructions on how to download and install pyinstaller, as well as how to use pyinstaller to package Python scripts into exe files. This includes downloading the pyinstaller suitable for the installed Python version (paying attention to choose between the stable or unstable version, as well as the operating system type), installing it by running the python setup.py install command, and packaging the script by using the icon=icon_path parameter.


      在我们完成一个Python项目或一个程序时,希望将Python的py文件打包成在Windows系统下直接可以运行的exe程序。在浏览网上的资料来看,有利用pyinstaller和cx_Freeze进行打包的方法。关于cx_Freeze打包的方由于方法主要是可以用于Python3.3版本的,高于该版本的Python无法使用。在我尝试的过程中,碰到的一个问题是:

安装cx_Freeze-4.3.3,找不到cxFreeze.bat

解决的方法是,在cmd命令行操作下,切换至放置cx_Freeze-4.3.3文件的Python安装目录下scripts目录下,使用以下代码自动生成:


python cxfreeze-postinstall


由于我安装的Python版本是3.6.2版本的,版本相对较新,cx_Freeze还没有更新版本至3.6版本。在我尝试很多次数失败后,决定采用pyinstaller来进行文件打包的方法。


下载pyinstaller

采用命令行操作的办法

在cmd命令行中,输入代码:

pip install pyinstaller

或者采用更新、升级的方法:

pip install --upgrade pyinstaller

采用下载文件+cmd操作的办法

首先,前往PyInstaller网站下载:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

upload_downloader_1745047901255_30572156.bmp

      下载时,要注意你安装的Python是多少版本的,有稳定版和不稳定版的,Python3.6只有不稳定版本的。其中绿色方框的是适用于linux操作系统下的,红色方框是适用于Windows系统下的。

进行解压缩、放置和安装

下载后,将压缩文件解压缩在Python安装目录下,进入该文件:

upload_downloader_1745047903214_77882373.bmp

该目录中含有XXXXXXXX,同时在cmd中进入该目录:

upload_downloader_1745047903481_81941086.bmp

输入命令:

python setup.py install

并按下回车键,会看到代码迅速执行,会进行安装pyinstaller,显示如下状态,看到“Finished processing dependencies for PyInstaller==3.3.dev0+41c426f6d”,即安装成功:

upload_downloader_1745047903786_16514092.bmp

使用pyinstaller打包py文件成exe程序

打包文件成exe程序

将cmd的目录切换至(命令:cd 文件路径(注意空格))需要打包的py文件目录下:

upload_downloader_1745047903044_62159899.bmp

并输入代码,格式为使用命令:pyinstaller -F 文件名(带后缀py):

pyinstaller -F test.py

常用参数 说明:
–icon=图标路径
-F 打包成一个exe文件
-w 使用窗口,无控制台
-c 使用控制台,无窗口
-D 创建一个目录,里面包含exe以及其他一些依赖性文件
pyinstaller -h 来查看参数
回车后,代码迅速操作,直到操作结束。

upload_downloader_1745047903465_16011640.jpeg



返回目标文件目录,发现该目录下生成了.spec文件test.spec:

upload_downloader_1745047903408_45199871.bmp

打包好的exe文件,在同目录的dist文件中:

upload_downloader_1745047903546_92098664.bmp

由此,py文件打包好为exe文件。

pyinstaller 改变生成exe程序的图标

pyinstaller -F --icon=my.ico test.py

my.ico 是一个图标名,和当前的XXXXXXX文件在同一个目录下

[ 修改于1年6个月前 - 2025/05/05 17:52:21 ]
1年6个月前IP:浙江
943403
1楼

不错,学到了

引用
评论
HSH作者
1年6个月前修改于 1年6个月前IP:山西
943452
2楼

另外再给大家提供一个火箭发动机推力计算器 ai写的(仅供娱乐)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

IMG_0852.jpeg

IMG_0854.jpeg

代码

attachment icon文本 2.txt11.59KBTXT1次下载

attachment icon文本.txt9.92KBTXT1次下载

引用
评论
3
HSH作者
1年5个月前IP:山西
944224
3楼

其实在编写程序,PC和VScode可以不使用,其python库可能安装失败,直接用python的IDE再按照python 库安装,简洁直接,只是稍微有点旧,做个小软件完美

引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的