Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
861 views
in Technique[技术] by (71.8m points)

python - Error in running exe file having xgboost package by using pyinstaller

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.

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:UsersShubhamSinghPycharmProjectscfna_scoringvenvlibsite- 
   packagesPyInstallerloaderpyimod03_importers.py", line 627, in 
  exec_module
  exec(bytecode, module.__dict__)
  File "libsite-packagesxgboost\__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:UsersShubhamSinghPycharmProjectscfna_scoringvenvlibsite- 
  packagesPyInstallerloaderpyimod03_importers.py", line 627, in 
  exec_module
  exec(bytecode, module.__dict__)
  File "libsite-packagesxgboostcore.py", line 161, in <module>
  File "libsite-packagesxgboostcore.py", line 123, in _load_lib
  File "libsite-packagesxgboostlibpath.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:UsersSHUBHA~1AppDataLocalTemp\_MEI11402xgboostxgboost.dll
  C:UsersSHUBHA~1AppDataLocalTemp\_MEI11402xgboost
  ../../lib/xgboost.dll
 C:UsersSHUBHA~1AppDataLocalTemp\_MEI11402xgboost./lib/xgboost.dll
  C:UsersSHUBHA~1AppDataLocalTemp\_MEI11402xgboostxgboost.dll
 C:UsersSHUBHA~1AppDataLocalTemp\_MEI11402xgboost
 ../../windows/x64/Release/xgboost.dll
   C:UsersSHUBHA~1AppDataLocalTemp\_MEI11402
  xgboost./windows/x64/Release/xgboost.dll
 [6564] Failed to execute script test_fraud

What's wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!")

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

Command:

(env) > pyinstaller myscript.py -F --add-data "./env/xgboost/*;xgboost/" --add-data "./env/Lib/site-packages/xgboost/VERSION;xgboost/"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...