背景
在代码中调用了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/