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

本文介绍了使用pyinstaller打包Python3程序时遇到的Nosuchfileordirectory和Cannotloadnativemodule错误的解决方案。针对这些问题,可以通过添加--add-binary参数指定缺失文件的位置,或者在入口文件中强制引入相关模块来解决。详细步骤包括分析错误信息、定位文件位置、修改打包命令和入口文件代码。

 

目录

实例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_shim
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "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 <

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值