Python项目打包成可执行的exe文件

简介: Python项目打包成可执行的exe文件

前言

之前有人私信我,他写了一个在终端交互的小程序,希望可以不在IDE的终端显示,而是独立一个窗口进行交互。其实只要把项目打包成exe执行文件,就可以在执行的时候在cmd独立一个窗口了。


如果python项目打包exe呢?下面按照步骤讲解一下。


环境依赖

安装pyinstaller。


pip install pyinstaller -i https://pypi.douban.com/simple

项目打包

项目部分代码


image.png


先看一下项目在终端执行的效果。


image.png


使用pyinstaller命令在打包将main方法打包。


pyinstaller -F main.py

打包过程

C:\Users\yi\PycharmProjects\txl>pyinstaller -F main.py
143 INFO: PyInstaller: 4.7
143 INFO: Python: 3.7.6
144 INFO: Platform: Windows-10-10.0.19041-SP0
145 INFO: wrote C:\Users\yi\PycharmProjects\txl\main.spec
162 INFO: UPX is not available.
165 INFO: Extending PYTHONPATH with paths
['C:\\Users\\yi\\PycharmProjects\\txl']
640 INFO: checking Analysis
641 INFO: Building Analysis because Analysis-00.toc is non existent
641 INFO: Initializing module dependency graph...
649 INFO: Caching module graph hooks...
696 INFO: Analyzing base_library.zip ...
4371 INFO: Caching module dependency graph...
4614 INFO: running Analysis Analysis-00.toc
4619 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\yi\appdata\local\programs\python\python37\python.exe
5046 INFO: Analyzing C:\Users\yi\PycharmProjects\txl\main.py
5049 INFO: Processing module hooks...
5050 INFO: Loading module hook 'hook-difflib.py' from 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
5052 INFO: Loading module hook 'hook-encodings.py' from 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
5253 INFO: Loading module hook 'hook-heapq.py' from 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
5256 INFO: Loading module hook 'hook-pickle.py' from 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
5260 INFO: Loading module hook 'hook-xml.py' from 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
5529 INFO: Looking for ctypes DLLs
5529 INFO: Analyzing run-time hooks ...
5532 INFO: Including run-time hook 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
5535 INFO: Including run-time hook 'c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
5542 INFO: Looking for dynamic libraries
5721 INFO: Looking for eggs
5722 INFO: Using Python library c:\users\yi\appdata\local\programs\python\python37\python37.dll
5722 INFO: Found binding redirects:
[]
5726 INFO: Warnings written to C:\Users\yi\PycharmProjects\txl\build\main\warn-main.txt
5765 INFO: Graph cross-reference written to C:\Users\yi\PycharmProjects\txl\build\main\xref-main.html
5782 INFO: checking PYZ
5783 INFO: Building PYZ because PYZ-00.toc is non existent
5783 INFO: Building PYZ (ZlibArchive) C:\Users\yi\PycharmProjects\txl\build\main\PYZ-00.pyz
6170 INFO: Building PYZ (ZlibArchive) C:\Users\yi\PycharmProjects\txl\build\main\PYZ-00.pyz completed successfully.
6182 INFO: checking PKG
6182 INFO: Building PKG because PKG-00.toc is non existent
6182 INFO: Building PKG (CArchive) main.pkg
7800 INFO: Building PKG (CArchive) main.pkg completed successfully.
7803 INFO: Bootloader c:\users\yi\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
7803 INFO: checking EXE
7803 INFO: Building EXE because EXE-00.toc is non existent
7804 INFO: Building EXE from EXE-00.toc
7805 INFO: Copying bootloader EXE to C:\Users\yi\PycharmProjects\txl\dist\main.exe
7897 INFO: Copying icon to EXE
7897 INFO: Copying icons from ['c:\\users\\yi\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
7996 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
7996 INFO: Writing RT_ICON 1 resource with 3752 bytes
7997 INFO: Writing RT_ICON 2 resource with 2216 bytes
7997 INFO: Writing RT_ICON 3 resource with 1384 bytes
7997 INFO: Writing RT_ICON 4 resource with 37019 bytes
7997 INFO: Writing RT_ICON 5 resource with 9640 bytes
7998 INFO: Writing RT_ICON 6 resource with 4264 bytes
7998 INFO: Writing RT_ICON 7 resource with 1128 bytes
8002 INFO: Copying 0 resources to EXE
8003 INFO: Emedding manifest in EXE
8004 INFO: Updating manifest in C:\Users\yi\PycharmProjects\txl\dist\main.exe
8007 INFO: Updating resource type 24 name 1 language 0
8011 INFO: Appending PKG archive to EXE
9702 INFO: Building EXE from EXE-00.toc completed successfully.
C:\Users\yi\PycharmProjects\txl>

会在dist生成exe文件,就是我们要的。

image.png

双击执行一下

image.png

总结

没啥总结的,做个记录。

分享:

       当你年轻时,以为什么都有答案,可是老了的时候,你可能又觉得其实人生并没有所谓的答案。——《天使之城》

相关文章
|
6天前
|
Linux iOS开发 MacOS
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
|
6天前
|
NoSQL Python
在Python中,我们可以使用许多库来处理Excel文件
Python处理Excel常用pandas和openpyxl库。pandas的`read_excel`用于读取文件,`to_excel`写入;示例展示了数据框操作。openpyxl则用于处理复杂情况,如多工作表,`load_workbook`加载文件,`iter_rows`读取数据,`Workbook`创建新文件,写入单元格数据后保存。
14 1
|
7天前
【Python21天学习挑战赛】文件读写操作
【Python21天学习挑战赛】文件读写操作
|
9天前
|
数据采集 存储 人工智能
Python采集数据保存CSV文件内容乱码解决
Python采集数据保存CSV文件内容乱码解决
24 1
|
9月前
|
小程序 Linux 区块链
Python PyInstaller 打包成 Win、Mac 应用程序(app / exe)
Python PyInstaller 打包成 Win、Mac 应用程序(app / exe)
579 0
|
5月前
|
小程序 Linux 区块链
Python PyInstaller 打包成 Win、Mac 应用程序(app / exe)
Python PyInstaller 打包成 Win、Mac 应用程序(app / exe)
150 0
|
4月前
|
安全 编译器 开发者
Python打包成.exe文件直接运行
Python打包成.exe文件直接运行
118 1
|
3月前
|
Python
使用PyInstaller将Python应用程序打包成EXE文件
使用PyInstaller将Python应用程序打包成EXE文件
181 0
|
3月前
|
Python
python 打包exe
python 打包exe
18 0
|
5月前
|
Python
Python 使用 pyinstaller 打包成 exe/app 文件,并支持传入外部参
Python 使用 pyinstaller 打包成 exe/app 文件,并支持传入外部参
60 0