目录

实例1.No such file or directory 错误  --add-binary 参数解决

实例2.Cannot load native module 错误 强制引入解决

总结


使用 pyinstaller 打包 python3 程序经常会出现 No such file or directory  或 Cannot load native module 错误 都是因为需要的文件未打入到最后的执行文件中,此时在使用pyinstaller时需要用到参数 --add-binary 以及在入口文件函数中加入使用代码 来解决

实例1.No such file or directory 错误  --add-binary 参数解决

打包入口python文件:

pyinstaller -F -w test.py

运行打包后的执行文件:

[root@0109c795032d src]# ./dist/test
Traceback (most recent call last):File "test.py", line 19, in <module>from salt.client.ssh import ssh_py_shimFile "<frozen importlib._bootstrap>", line 1007, in _find_and_loadFile "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 680, in _load_unlockedFile "PyInstaller/test/pyimod03_importers.py", line 540, in exec_moduleFile "salt/client/ssh/__init__.py", line 205, in <module>File "salt/utils/files.py", line 396, in fopen
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIudOUhL/salt/client/ssh/ssh_py_shim.pyc'
[5034] Failed to execute script test

通过报错可以看到是缺少文件,pyinstaller时增加 --add-binary 参数即可,具体如下:

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/salt/client/ssh/ssh_py_shim.py:salt/client/ssh"

参数解释:

--add-binary的语法是:--add-binary <SRC;DEST or SRC:DEST>

src是打包后的执行文件,缺少的文件在本机的位置,

dest是运行执行文件时,需要动态拷贝到的目录位置,dest是相对目录,

打包后的执行文件绝对路径 都是从 /tmp/_MEIudOUhL/ 开始的 这里是_MEIudOUhL是自动产生的每次不一样

dest只需要写相对目录即可,比如这里是 salt/client/ssh

最后的绝对路径就是 /tmp/_MEIudOUhL/salt/client/ssh

src和dest分隔符 linux下是冒号: windows下是分号;

添加此参数含义就是 把本机的 ssh_py_shim.py 文件打包到执行文件中,并且运行时动态释放到 相对目录 salt/client/ssh 中,从而解决 错误提示 的找不到文件问题

需要注意:

错误提示中报错找不到 ssh_py_shim.pyc 但此处引入的时 ssh_py_shim.py

这是因为 在整个函数调用栈中 倒数第二层 salt/client/ssh/__init__.py 里边有如下代码:

if not is_windows():shim_file = os.path.join(os.path.dirname(__file__), "ssh_py_shim.py")if not os.path.exists(shim_file):# On esky builds we only have the .pyc fileshim_file += "c"with salt.utils.files.fopen(shim_file) as ssh_py_shim:SSH_PY_SHIM = ssh_py_shim.read()

可以看到 ssh_py_shim.py 文件也是可以的,所以只要找到 ssh_py_shim.py 或者 ssh_py_shim.pyc 在 "salt/utils/files.py", line 396, 的 fopen 函数中都不会 出现找不到文件错误

官网实例:

https://pyinstaller.readthedocs.io/en/stable/usage.html#shortening-the-command

里边展示了 多个 --add-binary 和 其他相关参数的使用实例

实例2.Cannot load native module 错误 强制引入解决

pyinstaller 打包运行后错误如下:

  File "test/test.py", line 41, in init_testFile "salt/transport/client.py", line 27, in factoryFile "salt/utils/asynchronous.py", line 70, in __init__File "salt/transport/client.py", line 131, in factoryFile "<frozen importlib._bootstrap>", line 1007, in _find_and_loadFile "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 680, in _load_unlockedFile "PyInstaller/test/pyimod03_importers.py", line 540, in exec_moduleFile "salt/transport/zeromq.py", line 23, in <module>File "<frozen importlib._bootstrap>", line 1007, in _find_and_loadFile "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 680, in _load_unlockedFile "PyInstaller/test/pyimod03_importers.py", line 540, in exec_moduleFile "salt/crypt.py", line 65, in <module>File "<frozen importlib._bootstrap>", line 1007, in _find_and_loadFile "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 680, in _load_unlockedFile "PyInstaller/test/pyimod03_importers.py", line 540, in exec_moduleFile "Cryptodome/Cipher/__init__.py", line 27, in <module>File "<frozen importlib._bootstrap>", line 1007, in _find_and_loadFile "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 680, in _load_unlockedFile "PyInstaller/test/pyimod03_importers.py", line 540, in exec_moduleFile "Cryptodome/Cipher/_mode_ecb.py", line 35, in <module>File "Cryptodome/Util/_raw_api.py", line 297, in load_pycryptodome_raw_lib
OSError: Cannot load native module 'Cryptodome.Cipher._raw_ecb': Trying '_raw_ecb.cpython-39-x86_64-linux-gnu.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so', Trying '_raw_ecb.abi3.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so', Trying '_raw_ecb.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so'

此错误也开始缺少文件,缺少 _raw_ecb.cpython-39-x86_64-linux-gnu.so 或 _raw_ecb.so 首先加入 --add-binary

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/Crypto/Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so:Cryptodome/Util/../Cipher"

打包运行还是报此错误,反复检查 源文件 目标地址 都是正确的,

此时需要 复制在后边工程中引用 _raw_ecb 造成错误的代码 到入口函数

让pyinstaller充分知道我会使用 _raw_ecb.cpython-39-x86_64-linux-gnu.so 中的函数,从而强制打包此so文件

加入代码具体如下:

# 入口
if __name__ == "__main__":# 用于 pyinstaller 无法打包 salt 库try:ip = "127.0.0.1"port = 1234config = {'id': 'root','log_level': 'debug','master_ip': ip ,'master_port': port,'auth_timeout': 5,'auth_tries': 1,'master_uri': f'tcp://{ip}:{port}'}salt.transport.client.ReqChannel.factory(config, crypt='clear')except Exception as e:pass

再用pyinstaller打包

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/Crypto/Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so:Cryptodome/Util/../Cipher"

此时不用 --add-binary 也是应该可以的,未测试了

总结

出现错误 总体思路 还是通过函数调用栈来 倒推分析,

如果出现找不到文件情况 可以使用

1.--add-binary 参数

2.强制引入

来解决问题

python3 pyinstaller 打包后执行文件运行错误 No such file or directory 和 Cannot load native module 解决方法相关推荐

  1. 《无法打开包括文件:“Eigen\Dense”:No such file or directory》亲测有效的解决方法

    无法打开包括文件:"Eigen\Dense":No such file or directory 综述问题 解决方法 综述问题 Eigen是c++使用中常用的处理矩阵的工具包,通常 ...

  2. 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter...

    Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...

  3. 解决pyinstaller打包后在Win7运行提示缺少api-ms-win-crt*.dll

    第一次尝试 python版本为3.9 pip版本为21.2.3 pyinstaller版本为4.5.1 运行: pyinstaller -D -F -w xxx.py 在Win10平台打包成exe,且 ...

  4. python3 pyinstaller 打包后 程序会显示两个进程 解决方法

    问题 有一个基于 flask 框架开发的 web 项目, 通过 pyinstaller -F app.py打包并运行后, 发现有 2 个进程(mac, linux, windows 都是), 刚开始以 ...

  5. Linux微信运行错误,Linux安装wine微信安装和遇到的问题及解决方法

    如果系统之前安装过其他版本的wine,请先行卸载wine,之后在使用如下方法安装wine-5.0稳定版 wine完全卸载方法: 1.终端中执行sudo apt-get remove wine3.0(w ...

  6. Vue 打包后,使用火狐与谷歌可以打开index,但是IE小贱人打开空白的解决方法

    https://blog.csdn.net/Lou_Lan/article/details/101302364

  7. 解决pyinstaller打包后的exe文件打开闪退的问题

    解决pyinstaller打包后的exe文件打开闪退的问题 闪退问题:一般我们打包完后的exe文件点击运行就会直接闪退,很难看到具体错误 解决步骤: 首先打开 cmd 进入到 exe 文件所在目录(c ...

  8. Pyinstaller打包后的exe文件打开闪退解决方案

    Pyinstaller打包后的exe文件打开闪退解决方案 问题:利用pyinstaller对python代码打包后,dist文件夹中会生成一个xxx.exe可执行文件.但是很多朋友双击这个exe文件会 ...

  9. python-barcode 使用pyinstaller打包后运行报错cannot open resource

    python-barcode 使用pyinstaller打包后运行报错cannot open resource 问题描述: 使用pycharm可以正常生成条码,但是使用pyinstaller打包后,提 ...

最新文章

  1. 01-CoreData 概述
  2. 漫话:全球 IPv4 地址正式耗尽?是时候说说到底什么是IPv4和IPv6了!
  3. C++ 内存泄漏检测:valgrind和AddressSanitizer
  4. J20170521-ts
  5. 【机器视觉】 Halcon设置自动保存
  6. java中标准封装结构_java中的构造,封装
  7. 【python】yaml文件操作
  8. java中Error(UnsatisfiedLinkError)与Exception是有差异的
  9. atitit.web 推送实现方案集合(2)---百度云,jpush 极光推送 ,个推的选型比较.o99
  10. 【纯·干货】你会用到的期刊读Paper发论文写论文必备网站及各种小助手,不定期持续更新中~
  11. macos复制粘贴快捷键 快速_苹果电脑复制粘贴快捷键是什么 如何操作【图文】...
  12. python制作简易动态二维码
  13. 转换、刻录DVD影碟光盘教程
  14. arcmap10.7打开tif文件一片空白 | 解决方法
  15. 计算机科学与技术专业前沿技术相关论文,计算机科学与技术专业创新能力的培养途径论文...
  16. vue 仿豆瓣 爬坑之旅
  17. matplotlib——画布分辨率和尺寸
  18. 聚英国际|【燎原计划启动周第二期AMA】Filecoin经济模型及热点争议解读
  19. 用R语言拟合Eurogenes G25祖源坐标的学习笔记
  20. 如何把一个数组的值赋给另一个数组

热门文章

  1. eclipse的怪问题。background indexer crash recovery .java.lang.OutOfMemoryError: Java heap space
  2. 怎样用python模拟微信扫码登录_十一、模拟扫码登录微信(用Django简单的布置了下页面)发送接收消息...
  3. 微服务架构 — 微服务框架
  4. 使用MLX90640自制红外热像仪(一):MLX90640介绍与API库移植
  5. 字符串匹配的KMP算法和C语言代码,不需要思考就能理解
  6. 用keil烧写现成的hex文件
  7. Es6 generator浅入浅出
  8. promise-async-await
  9. Linux 自动重启进程
  10. ADO.NET笔记——带参数的查询防止SQL注入攻击