使用 pyinstaller 运行包含 xgboost 包的 exe 文件时出错 [英] Error in running exe file having xgboost package by using pyinstaller
问题描述
我有一个用于预测某些值的代码,该代码在代码中使用了 xgboost
包.当我在 PyCharm 中运行它时,它按预期运行.
I have a code for predicting some value that uses xgboost
package in the code. When I run it in PyCharm, it runs as expected.
问题是当我使用 pyinstaller
制作可执行文件时.它会使 exe
没有任何错误,但是当我运行它时,会出现以下错误:
The problem is when I make an executable file using pyinstaller
. It will make the exe
without any error, but when I run it the following error is raised:
Traceback (most recent call last):
File "test_fraud.py", line 3, in <module>
import xgboost
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File
"C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\__init__.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\core.py", line 161, in <module>
File "lib\site-packages\xgboost\core.py", line 123, in _load_lib
File "lib\site-packages\xgboost\libpath.py", line 48, in find_lib_path
xgboost.libpath.XGBoostLibraryNotFound: Cannot find XGBoost Library in
the candidate path, did you install compilers and run build.sh in root
path?
List of candidates:
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\./lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../windows/x64/Release/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\
xgboost\./windows/x64/Release/xgboost.dll
[6564] Failed to execute script test_fraud
这里出了什么问题?
推荐答案
Pyinstaller 似乎找不到 xgboost.dll
、VERSION
文件.因此,您需要手动将它们添加到您的输出包中.我还建议您使用 try/except
块来查看发生了什么.假设这个简单的例子:
It seems that Pyinstaller can't find the xgboost.dll
, VERSION
files. So you need to add them manually to your output package. I also suggest you use a try/except
block to see what is going on.
Suppose this simple example:
import traceback
try:
import xgboost
input("xgboost imported successfully!")
except Exception:
traceback.print_exc()
input("Import Error!")
我建议您使用 env 来构建您的脚本,因此,您需要添加位于
和 xgboost
目录;/xgboostVERSION
文件位于
.接下来,使用 pyinstaller 将它们添加为数据文件.启动您的 env 并执行以下命令(我的 virtualenv 名为 env
):
I suggest you use an env to build your script so, you need to add the xgboost
directory located at <path_to_venv>/xgboost
and VERSION
file located at <path_to_venv>/Lib/site-packages/xboost
. Next, add them as a data-file with pyinstaller. Launch your env and execute the below command (My virtualenv named as env
):
├───myscript.py
├───env
命令:
(env) > pyinstaller myscript.py -F --add-data "./env/xgboost/*;xgboost/" --add-data "./env/Lib/site-packages/xgboost/VERSION;xgboost/"
这篇关于使用 pyinstaller 运行包含 xgboost 包的 exe 文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!