【debug】pyinstaller 打包报错 xgboost.libpath.XGBoostLibraryNotFound: Cannot find XGBoost Library in the c

python打包报错 Cannot find XGBoost Library in the c

背景

在代码中调用了xgboost依赖包
在pyinstaller打包成文件夹后运行exe,报错 Cannot find XGBoost Library in the c。
报错信息:

  File "xgboost/core.py", line 231, in <module>
  File "xgboost/core.py", line 158, in _load_lib
  File "xgboost/libpath.py", line 64, in find_lib_path
xgboost.libpath.XGBoostLibraryNotFound: Cannot find XGBoost Library in the candidate path.  List of candidates:
- /home/Documents/xgboost/lib/libxgboost.so
- /home/Documents/xgboost/../../lib/libxgboost.so
- /home/Documents/lib/libxgboost.so
XGBoost Python package path: /home/Documents/xgboost
sys.prefix: /home/Documents/
See: https://xgboost.readthedocs.io/en/latest/build.html for installing XGBoost.

solution1

这里是网上介绍的一种方法,原文链接:
https://www.jianshu.com/p/f329ff972d6e
key points:
在spec文件中更改binaries,datas, hiddenimports三个参数

from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files
data = collect_data_files('xgboost')

a = Analysis(['filename.py'],
            pathex=['filepath\\scripts'],
            binaries = data,
            datas = data,
            hiddenimports= collect_submodules('xgboost'),
            ...)

然后再打包
pyinstaller xxx.spec
这种情况下会在dist/xxx/文件夹下生成xgboost的文件夹,但是我这边运行exe后还是报相同的错误,仔细看了一下,xgboost文件夹内只有两个文件。

solution2

找到自己使用的python环境的路径,我的是:
/home/edgeai/anaconda3/lib/python3.8/site-packages/
这个路径是我在运行pyinstaller的过程中可以看到,小伙伴们如果有其他方法能找到欢迎评论区留言。

ls | grep xgboost
xgboost
xgboost-1.6.2.dist-info
xgboost.libs

ls
callback.py  config.py  dask.py  __init__.py  libpath.py   __pycache__  rabit.py    tracker.py   _typing.py
compat.py    core.py    data.py  lib          plotting.py  py.typed     sklearn.py  training.py  VERSION

在这里的xgboost中我们可以看到不止2个文件,所以判断是打包时导入的xgboost文件夹内文件不全,有缺失,导致运行报错。
将这个文件夹粘贴到dist/xxx/中,再运行exe即可成功。

solution3

此处鸣谢 夕阳西下_繁星满天
如果在使用pyinstaller打包pypylon的时候,出现找不到相机的情况,可以在spec文档上加上:

import pypylon
import pathlib
pypylon_dir = pathlib.Path(pypylon.__file__).parent
pylon_dlls = [(str(dll), '.') for dll in pypylon_dir.glob('*.dll')]

notes

如果想要把一些资源文件在打包后copy到打包好的文件夹中,一方面可以在spec的binaries中填写。另一方面可以写个sh脚本,把要执行的cp的动作都写进去。
eg:
pyinstaller_need.sh

cp -r ./models/ ./dist/xx/
当你使用PyInstaller打包包含Qt库的应用时,错误消息`Could not find the Qt platform plugin "windows"`通常表示在打包过程中,PyInstaller没有找到Windows平台对应的Qt插件。Qt库依赖于特定平台的插件来提供图形界面支持。 以下是可能导致这个错误的一些原因和解决方法: 1. **Qt插件路径问题**:确保你的项目中已经包含了Qt的Windows平台插件(通常在`Qt安装目录/plugins/platforms`下)。在打包时,你需要将这个目录添加到PyInstaller的搜索路径中。你可以通过`--add-data`或`--paths`选项指定。 ```bash pyinstaller myapp.py --add-data="path/to/Qt/plugins/platforms;platforms" ``` 2. **环境变量设置**:检查系统环境变量`QT_PLUGIN_PATH`是否包含了正确的插件路径。如果打包是在不同的环境中运行,可能需要更新该环境变量。 3. **使用静链接**:Qt有时会提供静态链接的库,这可能会避免对动态插件的需求。确保你在配置中选择了正确的编译选项。 4. **Qt版本兼容性**:确认你的PyInstaller版本和Qt版本之间存在兼容性。不兼容的版本可能导致找不到正确的插件。 5. **重新配置PyInstaller**:如果你使用的是PyInstaller的`spec`文件,确保在`a.datas`部分正确配置了插件路径。 相关问题: 1. 如何检查和修改系统环境变量`QT_PLUGIN_PATH`? 2. PyInstaller如何处理不同版本的Qt库? 3. 如何确定我的应用是否适合使用静态链接的Qt库?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值