当前位置: 首页 > news >正文

【Python】如何使用PyInstaller打包自己写好的代码

使用PyInstaller打包自己写好的代码

零、需求

最近接到一个小单,需要批量修改文档内容,用Python做好后要打包成exe程序给客户的Win7电脑使用,此时需要用到PyInstaller打包自己的代码,想到还要有给用户试用的需求,所以还要加密打包。这里介绍一下如何打包并“加密”自己的Python程序。

壹、使用PyInstaller打包程序

建议:用Python开发程序时使用虚拟环境,打包也在虚拟环境中打包,这样减少项目之间的耦合性,方便管理。新建项目时新建虚拟环境

一、安装PyInstaller

安装之前建议把pip的源改为国内的,这样安装速度快很多,具体怎样更改这边不再赘述,安装pyinstaller命令如下(目前是2023年10月,我安装的是PyInstaller 6.0的版本):

pip install pyinstaller

安装过程如下:

(venv) PS D:\project\modify_docx_xlsx_left_header> pip install pyinstaller
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pyinstallerDownloading http://mirrors.aliyun.com/pypi/packages/a5/85/ddb59556f67ff274dd08201f0644a1715f245abc2d8b2faab7bd4e71b82d/pyinstaller-6.0.0-py3-none-win_amd64.whl (1.3 MB)---------------------------------------- 1.3/1.3 MB 1.1 MB/s eta 0:00:00
Collecting pywin32-ctypes>=0.2.1Downloading http://mirrors.aliyun.com/pypi/packages/a4/bc/78b2c00cc64c31dbb3be42a0e8600bcebc123ad338c3b714754d668c7c2d/pywin32_ctypes-0.2.2-py3-none-any.whl (30 kB)
Collecting importlib-metadata>=4.6Downloading http://mirrors.aliyun.com/pypi/packages/cc/37/db7ba97e676af155f5fcb1a35466f446eadc9104e25b83366e8088c9c926/importlib_metadata-6.8.0-py3-none-any.whl (22 kB)
Collecting pyinstaller-hooks-contrib>=2021.4Downloading http://mirrors.aliyun.com/pypi/packages/66/6b/d1e3b2c8d306694d2d95c4886aa3057c6ca680a759700ae924de84c488bc/pyinstaller_hooks_contrib-2023.9-py2.py3-none-any.whl (284 kB)---------------------------------------- 284.9/284.9 kB 2.9 MB/s eta 0:00:00
Collecting pefile>=2022.5.30Downloading http://mirrors.aliyun.com/pypi/packages/55/26/d0ad8b448476d0a1e8d3ea5622dc77b916db84c6aa3cb1e1c0965af948fc/pefile-2023.2.7-py3-none-any.whl (71 kB)---------------------------------------- 71.8/71.8 kB 232.3 kB/s eta 0:00:00
Collecting altgraphDownloading http://mirrors.aliyun.com/pypi/packages/4d/3f/3bc3f1d83f6e4a7fcb834d3720544ca597590425be5ba9db032b2bf322a2/altgraph-0.17.4-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: setuptools>=42.0.0 in d:\project\modify_docx_xlsx_left_header\venv\lib\site-packages (from pyinstaller) (65.5.1)
Collecting packaging>=20.0Downloading http://mirrors.aliyun.com/pypi/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl (53 kB)---------------------------------------- 53.0/53.0 kB 2.9 MB/s eta 0:00:00
Collecting zipp>=0.5Downloading http://mirrors.aliyun.com/pypi/packages/d9/66/48866fc6b158c81cc2bfecc04c480f105c6040e8b077bc54c634b4a67926/zipp-3.17.0-py3-none-any.whl (7.4 kB)
Installing collected packages: altgraph, zipp, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, packaging, importlib-metadata, pyinstaller
Successfully installed altgraph-0.17.4 importlib-metadata-6.8.0 packaging-23.2 pefile-2023.2.7 pyinstaller-6.0.0 pyinstaller-hooks-contrib-2023.9 pywin32-ctypes-0.2.2 zipp-3.17.0
WARNING: There was an error checking the latest version of pip.
(venv) PS D:\project\modify_docx_xlsx_left_header>

出现Successfully installed pyinstaller即安装成功。安装成功后建议把命令行程序关掉,然后重新打开,重新载入一下环境。

若是成功安装PyInstaller,在命令行程序中执行pyinstaller命令即可看到PyInstaller的帮助信息:

(venv) PS D:\project\modify_docx_xlsx_left_header> pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--contents-directory CONTENTS_DIRECTORY] [--add-data SOURCE:DEST] [--add-binary SOURCE:DEST] [-p DIR] [--hidden-import MODULENAME] [--collect-submodules MODULENAME] [--collect-data MODULENAME][--collect-binaries MODULENAME] [--collect-all MODULENAME] [--copy-metadata PACKAGENAME] [--recursive-copy-metadata PACKAGENAME] [--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES] [--splash IMAGE_FILE][-d {all,imports,bootloader,noarchive}] [--python-option PYTHON_OPTION] [-s] [--noupx] [--upx-exclude FILE] [-c] [-w] [--hide-console {hide-late,minimize-early,minimize-late,hide-early}] [-i <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">][--disable-windowed-traceback] [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE] [--uac-admin] [--uac-uiaccess] [--argv-emulation] [--osx-bundle-identifier BUNDLE_IDENTIFIER] [--target-architecture ARCH] [--codesign-identity IDENTITY][--osx-entitlements-file FILENAME] [--runtime-tmpdir PATH] [--bootloader-ignore-signals] [--distpath DIR] [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [--clean] [--log-level LEVEL]scriptname [scriptname ...]
pyinstaller: error: the following arguments are required: scriptname
(venv) PS D:\project\modify_docx_xlsx_left_header>

二、PyInstaller参数

主要参数如下:

参数说明
–distpath 目录应用程序包的存放位置(默认值:./dist)
–workpath 工作路径所有临时文件(.log和.pyz等)的存放位置(默认值:./build)
-D, --onedir创建一个包含可执行文件的单文件夹捆绑包,也就是打包成多个文件,适合以框架的形式编写工具代码,代码易于维护。(默认)
-F, --onefile打包成一个可执行文件,所有代码打包进一个文件中,方便分发使用。
-n 名字, --name 名字程序包和配置文件的名称(默认值:第一个脚本的名称,不含后缀)
–contents-directory 内容目录仅对于onedir构建(多文件打包)有效,指定所有支持文件(即除可执行文件本身之外的所有文件,依赖文件)将放置在其中的目录的名称。
–add-data SOURCE:DEST包含要添加到应用程序的数据文件的其他数据文件或目录。参数值应采用“source:dest_dir”的形式,其中source是要收集的文件(或目录)的路径,dest_dir是相对于顶级应用程序目录的目标目录,两个路径都用冒号(:)分隔。要将文件放在顶级应用程序目录中,请使用(.)作为dest_dir。此选项可以多次使用。
–add-binary SOURCE:DEST要添加到可执行文件中的其他二进制文件。请参见–add-data格式选项。此选项可以多次使用
-p 目录, --paths 目录搜索导入的路径(比如使用PYTHONPATH)。允许使用多个路径、由分隔或多次使用此选项。相当于在配置文件中提供参数。“:”pathex
–hidden-import 模块名称, --hiddenimport 模块名称在脚本代码中不可见的模块名称导入。此选项可以多次使用。
-w, --windowed, --noconsoleWindows和Mac OS X:不提供标准i/o的控制台窗口。在Mac操作系统上,这也会触发构建MacOS.app捆绑包。在Windows上,如果第一个脚本是“.pyw”文件,则会自动设置此选项。此选项在*NIX系统上被忽略。
-i <ico文件、exe文件,编号、icns文件、图片、“NONE”>, --icon <ico文件、exe文件,编号、icns文件、图片、“NONE”>ico文件:将图标应用于Windows可执行文件。exe文件,编号:从exe中提取对应编号的图标。icns文件:将图标应用于Mac OS上的.app捆绑包。如果输入的图像文件不是平台格式(Windows上为ico,Mac上为icns),PyInstaller会尝试使用Pillow将图标转换为正确的格式(如果安装了Pillow)。使用“NONE”不应用任何图标,从而使操作系统显示一些默认值(默认值:应用PyInstaller的图标)。此选项可以多次使用。

其他相关参数可以参考PyInstaller官网文档:PyInstaller参数说明。我们待会儿打包程序使用到的最多就是上面这些参数。

三、基本程序打包

因为我程序相对来讲比较小,不需要过多的依赖文件,故选择打成单文件包,打包成多文件一样的操作,就把参数-F改为-D即可。
我的程序目录如下:

MODIFY_DOCX_XLSX_LEFT_HEADER
│  app.py
│  config.py
│  gui.py
│  log.py
│  run.py
│
└─imagesicon.ico

其中run.py是入口文件。

使用如下命令将项目中所有代码打包成单文件:

pyinstaller -F run.py

打包过程如下:

(venv) PS D:\project\modify_docx_xlsx_left_header> pyinstaller -F run.py
608 INFO: PyInstaller: 6.0.0
608 INFO: Python: 3.8.10
624 INFO: Platform: Windows-7-6.1.7601-SP1
624 INFO: wrote D:\project\modify_docx_xlsx_left_header\run.spec
639 INFO: Extending PYTHONPATH with paths
['D:\\project\\modify_docx_xlsx_left_header']
1154 INFO: checking Analysis
1154 INFO: Building Analysis because Analysis-00.toc is non existent
1154 INFO: Initializing module dependency graph...
1170 INFO: Caching module graph hooks...
1185 INFO: Analyzing base_library.zip ...
2854 INFO: Loading module hook 'hook-heapq.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
3120 INFO: Loading module hook 'hook-encodings.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
5226 INFO: Loading module hook 'hook-pickle.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
6427 INFO: Caching module dependency graph...
6614 INFO: Running Analysis Analysis-00.toc
6614 INFO: Looking for Python shared library...
6676 INFO: Using Python shared library: C:\Program Files\Python38\python38.dll
6676 INFO: Analyzing D:\project\modify_docx_xlsx_left_header\run.py
7800 INFO: Loading module hook 'hook-charset_normalizer.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
8205 INFO: Loading module hook 'hook-certifi.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
8377 INFO: Loading module hook 'hook-docx.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
8548 INFO: Loading module hook 'hook-lxml.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
9859 INFO: Loading module hook 'hook-openpyxl.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
10545 INFO: Loading module hook 'hook-xml.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
10561 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
11606 INFO: Processing module hooks...
11622 INFO: Loading module hook 'hook-lxml.etree.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
11746 INFO: Loading module hook 'hook-difflib.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
12012 INFO: Loading module hook 'hook-platform.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
12043 INFO: Loading module hook 'hook-sysconfig.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
12542 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
13603 INFO: Loading module hook 'hook-lxml.isoschematron.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
13665 INFO: Loading module hook 'hook-_tkinter.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
13681 INFO: checking Tree
13681 INFO: Building Tree because Tree-00.toc is non existent
13681 INFO: Building Tree Tree-00.toc
13743 INFO: checking Tree
13743 INFO: Building Tree because Tree-01.toc is non existent
13743 INFO: Building Tree Tree-01.toc
13759 INFO: checking Tree
13759 INFO: Building Tree because Tree-02.toc is non existent
13759 INFO: Building Tree Tree-02.toc
13759 INFO: Loading module hook 'hook-lxml.objectify.py' from 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
14851 INFO: Looking for ctypes DLLs
14851 INFO: Analyzing run-time hooks ...
14851 INFO: Including run-time hook 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
14866 INFO: Including run-time hook 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
14866 INFO: Including run-time hook 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
14866 INFO: Including run-time hook 'D:\\project\\modify_docx_xlsx_left_header\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
15007 INFO: Looking for dynamic libraries
16738 INFO: Extra DLL search directories (AddDllDirectory): []
16738 INFO: Extra DLL search directories (PATH): []
18267 INFO: Warnings written to D:\project\modify_docx_xlsx_left_header\build\run\warn-run.txt
18408 INFO: Graph cross-reference written to D:\project\modify_docx_xlsx_left_header\build\run\xref-run.html
18470 INFO: checking PYZ
18470 INFO: Building PYZ because PYZ-00.toc is non existent
18470 INFO: Building PYZ (ZlibArchive) D:\project\modify_docx_xlsx_left_header\build\run\PYZ-00.pyz
19827 INFO: Building PYZ (ZlibArchive) D:\project\modify_docx_xlsx_left_header\build\run\PYZ-00.pyz completed successfully.
20046 INFO: checking PKG
20046 INFO: Building PKG because PKG-00.toc is non existent
20046 INFO: Building PKG (CArchive) run.pkg
26739 INFO: Building PKG (CArchive) run.pkg completed successfully.
26770 INFO: Bootloader D:\project\modify_docx_xlsx_left_header\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
26770 INFO: checking EXE
26770 INFO: Building EXE because EXE-00.toc is non existent
26770 INFO: Building EXE from EXE-00.toc
26770 INFO: Copying bootloader EXE to D:\project\modify_docx_xlsx_left_header\dist\run.exe
26786 INFO: Copying icon to EXE
26801 INFO: Copying 0 resources to EXE
26801 INFO: Embedding manifest in EXE
26801 INFO: Appending PKG archive to EXE
26879 INFO: Fixing EXE headers
27379 INFO: Building EXE from EXE-00.toc completed successfully.
(venv) PS D:\project\modify_docx_xlsx_left_header>

打包完成后在项目目录下生成了一个配置文件run.spec、一个输出目录dist和一个临时缓存目录build,其中打包好的可执行文件在输出目录dist中,没有特别指定名字,则是根据打包的脚本名命名的,我的为run.exe

四、资源打包

尝试运行run.exe,很遗憾,报错了,在弹出的一闪而过的控制台中可以看出,是图片资源找不到了:
资源找不到错误
我们需要把资源打包进来。

这需要在刚刚生成的配置文件中修改,或者使用--add-data参数(其实本质也是修改了配置文件),我使用参数的方式添加打包资源,使用如下命令:

pyinstaller -F run.py --add-data 'images/*:images'

根据上面参数的介绍,这个命令的意思是在将以run.py为入口的Python程序打成单个可执行文件的同时把目前相对位置为images文件夹下的所有文件嵌入到在执行时相对位置为images的文件夹中。要是有更多的资源文件夹或文件可以在后面继续添加--add-data参数。

但是这样还不行,还是提示文件找不到,经过了解,我们代码运行时的相对路径是以可执行文件位置作为起始地址的,而单个的可执行文件在运行前会把相关内容解压到电脑的缓存目录中,这样的话资源文件的位置就跟可执行文件位置不一样,从而找不到资源文件。还好,PyInstaller给我们提供了一个环境变量(PyInstaller官方文档 - 运行时信息),我们可以通过这个环境变量找到资源文件的位置,代码如下:

def get_res(file_path):import osimport sysif hasattr(sys, '_MEIPASS'):return os.path.join(getattr(sys, '_MEIPASS'), file_path)return file_path

通过这个函数获取到文件位置,然后再使用。

例如:
可以把

with open('res/a.txt', 'r', encoding='utf-8') as f:txt = f.read()

改写成

with open(get_res('res/a.txt'), 'r', encoding='utf-8') as f:txt = f.read()

这样就不会报错了。

注意:打包前需要把上次打包好的程序关掉,否则覆盖不了。

五、关闭控制台

发现序运行时会顺带开启一个控制台窗口,但是我的是图形化界面,不需要显示额外的控制台界面来调试。
默认开启控制台调试
使用-w参数关掉控制台:

pyinstaller -F run.py -w

合并参数如下:

pyinstaller -F run.py --add-data 'images/*:images' -w

重新打包后没有控制台界面了。

六、修改图标

我对默认的应用程序图标不太满意,我想使用我自己的图标,图标是项目的images文件夹下的icon.ico文件,可以使用-i参数指定可执行文件的图标:

pyinstaller -F run.py -i 'images/icon.ico'

-i后面跟着的是图标的路径,可以使用相对路径,也可以使用绝对路径。

合并参数如下:

pyinstaller -F run.py --add-data 'images/*:images' -w -i 'images/icon.ico'

运行此命令重新生成可执行文件后修改成功:
可执行文件图标
到这里,基本的打包需求就都满足了。

贰、使用CPython配合加密打包程序

参考资料

  1. 【python第三方库】pyinstaller使用教程及spec资源文件介绍
  2. pyinstaller打包python应用之方法(含打包图片资源)
  3. python 将资源文件打包进exe
  4. 谈谈 Pyinstaller 的编译和反编译,如何保护你的代码
  5. VS2015安装包-下载
  6. pip错误“Microsoft Visual C++ 14.0 is required.”解决办法
  7. Remove the --key/cipher bytecode encryption. #6999
  8. Visual Studio 较旧的下载 - 2019、2017、2015 和以前的版本
  9. Cython+Pyinstaller Python编译与打包-踩坑
  10. Oxyry Python Obfuscator
  11. python反编译 - 在线工具
  12. PyInstaller 官方文档
http://www.lryc.cn/news/183850.html

相关文章:

  • Java 线程的调度与时间片
  • Java项目-文件搜索工具
  • 记录开发中遇到关于MySQL的一些问题-MySQL版
  • 2023-10-06 LeetCode每日一题(买卖股票的最佳时机含手续费)
  • openGauss学习笔记-91 openGauss 数据库管理-内存优化表MOT管理-内存表特性-使用MOT-MOT使用MOT外部支持工具
  • PostgreSQL快速入门
  • MATLAB:线性系统的建模与仿真(含完整程序)
  • mycat实现mysql读写分离
  • 【C++】STL详解(十一)—— unordered_set、unordered_map的介绍及使用
  • 【C语言】动态通讯录(超详细)
  • Mac下docker安装MySQL8.0.34
  • 基于python编写的excel表格数据标记的exe文件
  • acwing算法基础之基础算法--高精度加法算法
  • openGauss学习笔记-84 openGauss 数据库管理-内存优化表MOT管理-内存表特性-MOT部署服务器优化:x86
  • 二分查找:34. 在排序数组中查找元素的第一个和最后一个位置
  • javaee ssm框架项目整合thymeleaf2.0 更多thymeleaf标签用法 项目结构图
  • lv7 嵌入式开发-网络编程开发 11 TCP管理与UDP协议
  • overleaf在线编辑工具使用教程
  • Python基础复习【第一弹】【黑马】
  • 【Word】公式编辑器中连字符/减号等显示偏长/过长
  • 架构设计系列4:如何设计高性能架构
  • 1392. 最长快乐前缀
  • 【C++设计模式之备忘录模式:行为型】分析及示例
  • 数据结构与算法(四):哈希表
  • FFmpeg 命令:从入门到精通 | ffplay 播放控制选项
  • 代码随想录day59
  • 【小工具-生成合并文件】使用python实现2个excel文件根据主键合并生成csv文件
  • 【论文阅读】An Evaluation of Concurrency Control with One Thousand Cores
  • 网页版”高德地图“如何设置默认城市?
  • 小谈设计模式(8)—代理模式