目录
实例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 <

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

被折叠的 条评论
为什么被折叠?



