Search code examples
pythonpyinstallerxgboost

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:\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

What's wrong here?


Solution

  • 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/"