【Python】如何将Python脚本打包成exe可执行文件

描述

相信大家也有过这样的困惑,本来已经使用Python写好了脚本代码,又不想公开代码逻辑,但又要给别人使用这些功能?

这可怎么办呢?

有没有什么办法,可以将Python脚本打包成一个exe文件,这样发布的时候,我只需要提交一个exe文件,别人只要双击打开就能使用我的功能,而我又不需要公开代码,且不一举两得?

答案肯定是有的,请看下文分解。


这里我随意编写了一个Python脚本作为例子来介绍,功能很简单,就是每秒钟输出一个索引值,值是递加1的,代码如下:

import time

index = 1

print('start running')

for index in range(1,1000):
	print(index)
	index = index + 1
	time.sleep(1)

print('stop running ...')
脚本

使用Python命令行运行的结果如下:

脚本

 


那么我们使用工具把这个Python代码打包成一个exe文件。

这里需要用到一个Python的工具包,交Pyinstaller,我们使用以下命令就可以安装它。

pip install pyinstaller
脚本

 如下所示:(黄色字体提示pip有新版本要更新,可以不用管他)

脚本

 

提示安装成功后,输入下面的命令,有正常输出表示pyinstaller安装成功了。

pyinstaller.exe --help
脚本脚本

 

 下面我们就开始使用这个工具将python脚本打包成exe文件。

首先,我们进到Python脚本文件所在的目录,如下:(我的是powershell命令行环境) 

脚本

 

 接着使用命令开始打包Python脚本,交互输出log如下所示:

PS Z:win_share_workspacetest_sharetest_python2exe> pyinstaller.exe -F .running.py
179 INFO: PyInstaller: 4.5.1
180 INFO: Python: 3.7.8
188 INFO: Platform: Windows-10-10.0.19041-SP0
316 INFO: wrote Z:win_share_workspacetest_sharetest_python2exerunning.spec
501 INFO: UPX is not available.
1113 INFO: Extending PYTHONPATH with paths
['Z:\win_share_workspace\test_share\test_python2exe',
 'Z:\win_share_workspace\test_share\test_python2exe']
6010 INFO: checking Analysis
6047 INFO: Building Analysis because Analysis-00.toc is non existent
6048 INFO: Initializing module dependency graph...
6119 INFO: Caching module graph hooks...
6148 INFO: Analyzing base_library.zip ...
13118 INFO: Processing pre-find module path hook distutils from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
13120 INFO: distutils: retargeting to non-venv dir 'c:\users\redtea\appdata\local\programs\python\python37\lib'
15205 INFO: Caching module dependency graph...
16314 INFO: running Analysis Analysis-00.toc
16322 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:usersredteaappdatalocalprogramspythonpython37python.exe
18942 INFO: Analyzing Z:win_share_workspacetest_sharetest_python2exerunning.py
19151 INFO: Processing module hooks...
19153 INFO: Loading module hook 'hook-difflib.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
19155 INFO: Loading module hook 'hook-distutils.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
19157 INFO: Loading module hook 'hook-encodings.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20863 INFO: Loading module hook 'hook-heapq.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20865 INFO: Loading module hook 'hook-pickle.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20867 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20869 INFO: Loading module hook 'hook-xml.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
21563 INFO: Looking for ctypes DLLs
21564 INFO: Analyzing run-time hooks ...
21567 INFO: Including run-time hook 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
21636 INFO: Including run-time hook 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
21642 INFO: Looking for dynamic libraries
22600 INFO: Looking for eggs
22600 INFO: Using Python library c:usersredteaappdatalocalprogramspythonpython37python37.dll
22600 INFO: Found binding redirects:
[]
22748 INFO: Warnings written to Z:win_share_workspacetest_sharetest_python2exebuildrunningwarn-running.txt
23472 INFO: Graph cross-reference written to Z:win_share_workspacetest_sharetest_python2exebuildrunningxref-running.html
23842 INFO: checking PYZ
23921 INFO: Building PYZ because PYZ-00.toc is non existent
23921 INFO: Building PYZ (ZlibArchive) Z:win_share_workspacetest_sharetest_python2exebuildrunningPYZ-00.pyz
26078 INFO: Building PYZ (ZlibArchive) Z:win_share_workspacetest_sharetest_python2exebuildrunningPYZ-00.pyz completed successfully.
26806 INFO: checking PKG
26830 INFO: Building PKG because PKG-00.toc is non existent
26830 INFO: Building PKG (CArchive) PKG-00.pkg
48506 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
48830 INFO: Bootloader c:usersredteaappdatalocalprogramspythonpython37libsite-packagesPyInstallerbootloaderWindows-64bitrun.exe
48830 INFO: checking EXE
48912 INFO: Building EXE because EXE-00.toc is non existent
48913 INFO: Building EXE from EXE-00.toc
49652 INFO: Copying icons from ['c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\images\icon-console.ico']
50456 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
50456 INFO: Writing RT_ICON 1 resource with 3752 bytes
50457 INFO: Writing RT_ICON 2 resource with 2216 bytes
50458 INFO: Writing RT_ICON 3 resource with 1384 bytes
50458 INFO: Writing RT_ICON 4 resource with 37019 bytes
50458 INFO: Writing RT_ICON 5 resource with 9640 bytes
50459 INFO: Writing RT_ICON 6 resource with 4264 bytes
50459 INFO: Writing RT_ICON 7 resource with 1128 bytes
52342 INFO: Updating manifest in Z:win_share_workspacetest_sharetest_python2exebuildrunningrun.exe.z10cqb8f
54272 INFO: Updating resource type 24 name 1 language 0
56760 INFO: Appending archive to EXE Z:win_share_workspacetest_sharetest_python2exedistrunning.exe
155327 INFO: Building EXE from EXE-00.toc completed successfully.
脚本

看到最后提示successfully,就表示打包成功了。生成的exew文件位于dist目录,我们打开看看。

脚本

 

打包出来的文件还是毕竟大,简单几行代码就达到了6MB,主要是因为它把一些Python的依赖库也打包进来了,所以我们双击就可以运行,运行效果与Python命令行运行一模一样。

脚本

好了,本期将Python脚本打包成exe文件的教程到这里就结束了,你学会了吗?

更多实用教程,请关注我的主页,谢谢大家。 

​  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分