100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 【Python实用技巧】如何将Python脚本打包成exe可执行文件?

【Python实用技巧】如何将Python脚本打包成exe可执行文件?

时间:2021-08-12 02:09:45

相关推荐

【Python实用技巧】如何将Python脚本打包成exe可执行文件?

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

这可怎么办呢?

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

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

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

import timeindex = 1print('start running')for index in range(1,1000):print(index)index = index + 1time.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_workspace\test_share\test_python2exe> pyinstaller.exe -F .\running.py179 INFO: PyInstaller: 4.5.1180 INFO: Python: 3.7.8188 INFO: Platform: Windows-10-10.0.19041-SP0316 INFO: wrote Z:\win_share_workspace\test_share\test_python2exe\running.spec501 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 Analysis6047 INFO: Building Analysis because Analysis-00.toc is non existent6048 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.toc16322 INFO: Adding mon-Controls to dependent assemblies of final executablerequired by c:\users\redtea\appdata\local\programs\python\python37\python.exe18942 INFO: Analyzing Z:\win_share_workspace\test_share\test_python2exe\running.py19151 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 DLLs21564 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 libraries22600 INFO: Looking for eggs22600 INFO: Using Python library c:\users\redtea\appdata\local\programs\python\python37\python37.dll22600 INFO: Found binding redirects:[]22748 INFO: Warnings written to Z:\win_share_workspace\test_share\test_python2exe\build\running\warn-running.txt23472 INFO: Graph cross-reference written to Z:\win_share_workspace\test_share\test_python2exe\build\running\xref-running.html23842 INFO: checking PYZ23921 INFO: Building PYZ because PYZ-00.toc is non existent23921 INFO: Building PYZ (ZlibArchive) Z:\win_share_workspace\test_share\test_python2exe\build\running\PYZ-00.pyz26078 INFO: Building PYZ (ZlibArchive) Z:\win_share_workspace\test_share\test_python2exe\build\running\PYZ-00.pyz completed successfully.26806 INFO: checking PKG26830 INFO: Building PKG because PKG-00.toc is non existent26830 INFO: Building PKG (CArchive) PKG-00.pkg48506 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.48830 INFO: Bootloader c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe48830 INFO: checking EXE48912 INFO: Building EXE because EXE-00.toc is non existent48913 INFO: Building EXE from EXE-00.toc49652 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 bytes50456 INFO: Writing RT_ICON 1 resource with 3752 bytes50457 INFO: Writing RT_ICON 2 resource with 2216 bytes50458 INFO: Writing RT_ICON 3 resource with 1384 bytes50458 INFO: Writing RT_ICON 4 resource with 37019 bytes50458 INFO: Writing RT_ICON 5 resource with 9640 bytes50459 INFO: Writing RT_ICON 6 resource with 4264 bytes50459 INFO: Writing RT_ICON 7 resource with 1128 bytes52342 INFO: Updating manifest in Z:\win_share_workspace\test_share\test_python2exe\build\running\run.exe.z10cqb8f54272 INFO: Updating resource type 24 name 1 language 056760 INFO: Appending archive to EXE Z:\win_share_workspace\test_share\test_python2exe\dist\running.exe155327 INFO: Building EXE from EXE-00.toc completed successfully.

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

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。