Python pyinstaller类库使用学习总结

news/2025/1/24 16:40:56/文章来源:https://www.cnblogs.com/shouke/p/18509425

实践环境

python3 .9.13

pyinstaller-6.10.0-py3-none-manylinux2014_x86_64.whl

CentOS 7.9

win11

实践操作

生成Linux版可执行文件

安装Python

# yum install -y gcc zlib* openssl-devel libffi-devel
# wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz
# mkdir -p /usr/local/python
# mv Python-3.9.13.tgz /usr/local/python
# tar -xvzf Python-3.9.13.tgz
# cd python-3.9.13
# ./configure --prefix=/usr/local/python/python3.9.13 --enable-shared
# make && make install

说明:

1、如果不安装libffi-devel,运行pyinstaller时会报错:ModuleNotFoundError: No module named '_ctypes',以下

# pyinstaller simulator.py
Traceback (most recent call last):File "/usr/bin/pyinstaller", line 8, in <module>sys.exit(_console_script_run())File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 231, in _console_script_runrun()File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 172, in runparser = generate_parser()File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 137, in generate_parserimport PyInstaller.building.build_mainFile "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 28, in <module>from PyInstaller.building.api import COLLECT, EXE, MERGE, PYZFile "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/api.py", line 33, in <module>from PyInstaller.building.splash import Splash  # argument type validation in EXEFile "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/splash.py", line 23, in <module>from PyInstaller.depend import bindependFile "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/depend/bindepend.py", line 15, in <module>import ctypes.utilFile "/usr/local/python/python3.9.13/lib/python3.9/ctypes/__init__.py", line 8, in <module>from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

2、如果编译Python时不携带--enable-shared选项,运行pyinstaller时会报类似如下错误:

  File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 671, in assembleraise PythonLibraryNotFoundError()
PyInstaller.exceptions.PythonLibraryNotFoundError: Python library not found: libpython3.9.so.1.0, libpython3.9.soThis means your Python installation does not come with proper shared library files.This usually happens due to missing development package, or unsuitable build parameters of the Python installation.* On Debian/Ubuntu, you need to install Python development packages:* apt-get install python3-dev* apt-get install python-dev* If you are building Python by yourself, rebuild with `--enable-shared` (or, `--enable-framework` on macOS).

添加/usr/lib64/libpython3.9.so.1.0文件

# find / -name libpython3.9.so.1.0
/root/Python-3.9.13/libpython3.9.so.1.0
/usr/local/python/python3.9.13/lib/libpython3.9.so.1.0
# cp /usr/local/python/python3.9.13/lib/libpython3.9.so.1.0 /usr/lib64/libpython3.9.so.1.0

说明:如果不执行该操作,运行pyinstaller时生成可执行文件时可能报类似如下错误:

# pyinstaller simulator.py
/usr/local/python/python3.9.13/bin/python3.9: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory

安装 pyinstaller

# pip3 install pyinstaller -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

创建软连接,避免直接执行 pyinstaller 命令时,提示 pyinstaller: command not found

# find / -name pyinstaller
/usr/local/python/python3.9.13/bin/pyinstaller
# 
# ln -fs /usr/local/python/python3.9.13/bin/pyinstaller /usr/binpyinstaller
# pyinstaller -v
6.10.0

生成可执行文件

# pwd
/root/au02Simulator
# pyinstaller -y  simulator.py
87 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
87 INFO: Python: 3.9.13
89 INFO: Platform: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17
89 INFO: Python environment: /usr/local/python/python3.9.13
90 INFO: wrote /root/au02Simulator/simulator.spec
93 INFO: Module search paths (PYTHONPATH):
['/usr/local/python/python3.9.13/lib/python39.zip','/usr/local/python/python3.9.13/lib/python3.9','/usr/local/python/python3.9.13/lib/python3.9/lib-dynload','/usr/local/python/python3.9.13/lib/python3.9/site-packages','/root/au02Simulator']
166 INFO: checking Analysis
172 INFO: checking PYZ
181 INFO: checking PKG
181 INFO: Bootloader /usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
181 INFO: checking EXE
182 INFO: checking COLLECT
182 INFO: Building COLLECT COLLECT-00.toc
210 INFO: Building COLLECT COLLECT-00.toc completed successfully.
# cd dist/
# ll
total 0
drwxr-xr-x. 3 root root 40 Oct 24 12:46 simulator
# cd simulator/
# ll
total 1204
drwxr-xr-x. 4 root root    4096 Oct 24 12:46 _internal
-rwxr-xr-x. 1 root root 1227016 Oct 24 12:46 simulator

说明:这里的simulator.py为程序入口文件,即用python运行本程序时,位于其后的.py文件

只生成一个文件

# pyinstaller -y --onefile simulator.py
74 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
75 INFO: Python: 3.9.13
76 INFO: Platform: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17
76 INFO: Python environment: /usr/local/python/python3.9.13
77 INFO: wrote /root/au02Simulator/simulator.spec
79 INFO: Module search paths (PYTHONPATH):
['/usr/local/python/python3.9.13/lib/python39.zip','/usr/local/python/python3.9.13/lib/python3.9','/usr/local/python/python3.9.13/lib/python3.9/lib-dynload','/usr/local/python/python3.9.13/lib/python3.9/site-packages','/root/au02Simulator']
146 INFO: checking Analysis
149 INFO: checking PYZ
157 INFO: checking PKG
158 INFO: Building because toc changed
158 INFO: Building PKG (CArchive) simulator.pkg
7881 INFO: Building PKG (CArchive) simulator.pkg completed successfully.
7882 INFO: Bootloader /usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
7882 INFO: checking EXE
7883 INFO: Rebuilding EXE-00.toc because simulator missing
7883 INFO: Building EXE from EXE-00.toc
7883 INFO: Copying bootloader EXE to /root/au02Simulator/dist/simulator
7883 INFO: Appending PKG archive to custom ELF section in EXE
7900 INFO: Building EXE from EXE-00.toc completed successfully.
# cd dist/
# ll
total 12444
-rwxr-xr-x. 1 root root 12741136 Oct 24 12:50 simulator

注意,生成的文件是不带配置的,程序所需配置需要自己添加

生成Windows版可执行文件

安装 pyinstaller

同Linux,安装完成后,会在${PYTHON_HOME}\Scripts目录下生成pyinstaller.exe,为方便执行,将其所在路径添加到环境变量

生成可执行文件

同Linux

pyinstaller [选项] program_entry_file.py

pyinstaller常用选项

  • -y 档输出目录下的存在旧生成文件时,不提示删除确认提示,直接删除

  • -F,--onefile 打包为一个独立文件

  • --add-data <source>;<dest_dir> (适用Win) --add-data <source>:<dest_dir> (适用Linux) 用于将非python文件如配置,打包进程序。

    • <source>是资源文件的路径,可以是相对路径或绝对路径,建议使用绝对路径。
    • <dest_dir>是资源文件在打包后在可执行文件中的目标目录路径。是相对于应用程序顶层目录的目标目录,如果要将文件放入应用程序顶层目录,则使用 . 作为 <dest_dir>。注意:如果该目录路径不存在,则会自动创建

    --add-data 可重复使用,以支持单次添加多个文件的需求

例子:

假设我们有一个Python项目,其中包含一个名为config.ini的配置文件。希望在使用PyInstaller打包项目时,将这个配置文件也一起打包进去。

cd /d D:\PyProjects\ZenTaoTestcaseHelper
pyinstaller --add-data "D:\PyProjects\ZenTaoTestcaseHelper\config.json;." testcase_helper.py

生成目标结果文件路径如下:

D:\PyProjects\ZenTaoTestcaseHelper\dist\testcase_helper\testcase_helper.exe
D:\PyProjects\ZenTaoTestcaseHelper\dist\testcase_helper\config.json

运行可执行文件无法正确读取配置文件解决方案

实践时遇到过这样的情况:

直接使用python program_entry_file.py的方式运行程序时,可正确执行不报错,直接运行打包生成的可执行文件时,出现报错,提示相关配置文件不存在。

经过分析发现,直接运行可执行文件时,读取配置文件的路径不对,为了避免这种情况,可在代码中添加判断,设置采用可执行文件的方式运行时的配置文件读取路径:

program_entry_file.py(程序入口文件)中判断是否是运行打包生成的可执行文件,如果是,则设置环境变量,获取可执行文件所在目录

sys_executable = sys.executable
if not sys_executable.endswith('python.exe') and not re.findall('python\d*$', sys_executable):os.environ['EXECUTABLE_DIR'] = os.path.dirname(sys.executable)

其它需要读取配置文件的py文件中添加判断

假设我们期望可执行文件从位于同级目录的conf目录下的program.conf文件中读取配置

config_file_path = 'conf/program.conf'
if os.getenv('EXECUTABLE_DIR'):config_file_path = os.path.join(os.environ['EXECUTABLE_DIR'], config_file_path)

参考链接

https://pyinstaller.org/en/v6.2.0/usage.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/823014.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言中实现4行3列矩阵和3行4列矩阵的运算

001、[root@PC1 test]# ls test.c [root@PC1 test]# cat test.c ## 测试c程序 #include <stdio.h>int main(void) {int i,j,k;int v1[4][3];int v2[3][4];int v3[4][4] = {{}, {}, {}, {}}; //4行3列矩阵与3行4列矩阵的乘积是…

11.索引概念

索引:作用:提高查询效率 索引不止存在内存中,还要写到磁盘上常见的索引模型:哈希表:适用于只有等值查询的场景(kv键值对) 有序数组:等值查询和范围查询场景(redis的有序集合) 搜索树:二叉树是搜索效率最高,大多数的数据库存储却并不使用二叉树 > [!TIP] 索引不止…

3DCAT亮相2024中国国际消费电子博览会,引领AI潮流

2024年10月18日-20日,备受瞩目的2024中国国际消费电子博览会在青岛盛大开幕.在电博会现场,瑞云科技通过实际案例展示了如何通过3DCAT XR实时云渲染及AI多模态大模型解决方案的强大功能和广泛应用,帮助企业实现数字化转型和创新发展.2024年10月18日-20日,备受瞩目的2024中国国际…

0.学习路径

学习路径: 阶段一:算法入门我们需要熟悉各种数据结构的特点和用法,学习不同算法的原理、流程、用途和效率等方面的内容。阶段二:刷算法题建议从热门题目开刷,先积累至少 100 道题目,熟悉主流的算法问题。初次刷题时,“知识遗忘”可能是一个挑战,但请放心,这是很正常的…

习题2.11

import string import random x = string.ascii_letters + string.digits y=.join([random.choice(x) for i in range(1000)]) d = dict() for ch in y: d[ch] = d.get(ch,0) + 1; for k,v in sorted(d.items()): print(k,:,v)

2.4习题

-- coding: utf-8 -- """ Created on Tue Sep 17 19:39:40 2024 @author: 朱尧 """ a = [[1,2,3],[4,5,6],[7,8,9]] d = [c for b in a for c in b] print(d)

20222304 2024-2025-1 《网络与系统攻防技术》实验三实验报告

实验内容 1.1 实践内容 正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧 使用msfvenom生成jar、apk等其他文件 使用veil加壳工具 使用C+ shellcode进行编程 通过组合应用各种技术实现恶意代码免杀 用另一电脑实测,在杀软开启的情况下,可运行并回连成…

软考刷题记录4

IPSec 用于增强 IP 网络的安全性,下面的说法中不正确的是( )。 A.IPSec 可对数据进行完整性保护 B.IPSec提供用户身份认证服务 C.IPSec的认证头添加在TCP封装内部 D.IPSec对数据加密传输 【正确答案】:C 【试题解析】: 在传输模式下,IPsec包头增加在原IP包头和数据之间,…

2.34

程序文件ex2_34.py import numpy as np a = np.array([[3, 1], [1, 2]]) b = np.array([9, 8]) x1 = np.linalg.inv(a) @ b #第一种解法 上面语句中@表示矩阵乘法 x2 = np.linalg.solve(a, b) #第二种解法 print(x1); print(x2)

2.26

import numpy as np a = np.ones(4, dtype= int) b = np.ones((4,), dtype= int) c = np.ones((4,1)) d = np.zeros(4) e = np.empty(3) f = np.eye(3) g = np.eye(3, k = 1) h = np.zeros_like(a) print(a);print(b);print(c);print(d);print(e);print(f);print(g);print(h)

陶麟鑫第二次作业

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zjlg/rjjc这个作业的目标 实现一个命令行文本计数统计程序。能正确统计导入的纯英文txt文本中的字符数,单词数,句子数。姓名-学号 陶麟鑫 2022329301019Gitee仓库地址: https://gitee.com/tao-linxin/wold_-counter P…

2.20

x1 = "abcde" x2 = list(enumerate(x1)) for ind, ch in enumerate(x1): print(ch)