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

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?

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

Related

WinError Converting python script to executable with asyncio lib and pyinstaller

I am trying to build a python executable for Windows OS with means of pyinstaller but it does not work.
Here is all the code:
import asyncio
import os
import time
def main():
print("Hello World!")
time.sleep(4)
if __name__ == '__main__':
os.system('cls')
main()
PyInstaller command: python -m PyInstaller main.py --name asyc_test --clean --onefile -w
System: Windows 10 OS, Intel 64bit, Python 3.10.2, pyinstaller 5.4.1
Error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "asyncio\__init__.py", line 39, in <module>
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "asyncio\windows_events.py", line 8, in <module>
OSError: [WinError 10022] An invalid argument was supplied
I know that errors root lays in asyncio and already found this related issue, but i am not able to produce a fix. Anyone any ideas?
Thanks in advance!

NameError: name 'defaultParams' is not defined while running the .exe converted using Pyinstaller

I successfully converted the main.py script using pyinstaller. However the below stated error is thrown while executing the .exe file for the same.
MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.
exec(bytecode, module.__dict__)
Traceback (most recent call last):
File "main.py", line 8, 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\XXXX\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "matplotlib\__init__.py", line 898, in <module>
File "matplotlib\cbook\__init__.py", line 480, in _get_data_path
File "matplotlib\__init__.py", line 239, in wrapper
File "matplotlib\__init__.py", line 534, in get_data_path
File "matplotlib\__init__.py", line 239, in wrapper
File "matplotlib\__init__.py", line 563, in _get_data_path
NameError: name 'defaultParams' is not defined
Supposedly, all you can do to fix this is to downgrade your matplotlib to the version:
pip install matplotlib==3.2.2
Apparently, the latest version of matplotlib meddles with the environment variable in the windows path.

"Failed to load dynlib/dll" While executing the "*.exe " file after binding it with PyInstaller

The code successfully works while executing using CMD , but when it is binded to *.exe file using Pyinstaller, It throws an error and stops executing (the Message is given below). I tried all possible solutions by linking the path library and other stuff too.
MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
exec(bytecode, module.__dict__)
Traceback (most recent call last):
File "site-packages\pyinstaller-4.0.dev0-py3.7.egg\PyInstaller\loader\pyiboot01_bootstrap.py", line 167, in __init__
File "ctypes\__init__.py", line 364, in __init__
OSError: [WinError 126] The specified module could not be found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "try.py", line 2, 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\Mohammed Hashim\AppData\Local\Programs\Python\Python37\lib\site-packages\pyinstaller-4.0.dev0-py3.7.egg\PyInstaller\loader\pyimod03_importers.py", line 489, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\__init__.py", line 80, 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\Mohammed Hashim\AppData\Local\Programs\Python\Python37\lib\site-packages\pyinstaller-4.0.dev0-py3.7.egg\PyInstaller\loader\pyimod03_importers.py", line 489, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\_distributor_init.py", line 18, in <module>
File "site-packages\pyinstaller-4.0.dev0-py3.7.egg\PyInstaller\loader\pyiboot01_bootstrap.py", line 169, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\MOHAMM~1\\AppData\\Local\\Temp\\_MEI38242\\sklearn\\.libs\\vcomp140.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[13968] Failed to execute script try
Previously , It was [11496] Failed to execute script try with similar error message.
I also hereby share my code. (this code was written to check the missing library and Error Message - Handy error detection)
import pandas as pd
from sklearn import datasets
data = pd.read_csv(r"D:\PROGRAMS\DATASETS\Kang_nose_data_csv.csv")
print(data.head())
It produces similar error for sklearn,numpy and pandas and many other libraries.
Thanks...!

Missing dill was found using pyinstaller to compile executable file

I built a small program using sklearn, numpy and pandas, I tried to compile the file but it failed and have the following error message, it said there was a missing dll file in some location. I looked at this thread.
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 174, in __init__
File "ctypes\__init__.py", line 373, in __init__
FileNotFoundError: Could not find module 'C:\Users\Dennis\AppData\Local\Temp\_MEI101722\sklearn\.libs\vcomp140.dll' (or one of its dependencies). Try using the full path with constructor syntax.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sklearn111.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "c:\users\dennis\appdata\local\programs\python\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\__init__.py", line 78, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "c:\users\dennis\appdata\local\programs\python\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\_distributor_init.py", line 18, in <module>
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 176, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\Dennis\\AppData\\Local\\Temp\\_MEI101722\\sklearn\\.libs\\vcomp140.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[6572] Failed to execute script gui_plus_model
So I put the dll it needs at the same folder with the .py file to be compiled, but the same error exist. The version of python is 3.8 and all modules needed have already been upgraded to the latest version.
pyinstaller -F --add-binary "vcomp140.dll;.dll" gui_plus_model.py

How to add lib_lightgbm.dll to exe when using pyinstaller to freeze python code

I am creating a single-file executable of a python code that imports Microsoft's LightGBM. I used the following command to create the exe:
pyinstaller -F --hidden-import numpy.core._dtype_ctypes pro.py
After creating the exe and running it, the following error occurs:
Exception: Cannot find lightgbm library file in following paths:
C:\Users\...\AppData\Local\Temp\_MEI61682\lightgbm\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\lightgbm\compile\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\lib\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\Release\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\windows\x64\DLL\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\Release\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\windows\x64\DLL\lib_lightgbm.dll
[6352] Failed to execute script pro
I tried adding lib_lightgbm.dll using the option :
--add-data lib_lightgbm.dll;\compile
This resolves the error on the machine that lightgbm is installed, but on other machines, it gives the following error:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 149, in __init__
File "ctypes\__init__.py", line 348, in __init__
OSError: [WinError 126] The specified module could not be found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pro.py", line 9, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\...\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
File "site-packages\lightgbm\__init__.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\...\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
File "site-packages\lightgbm\basic.py", line 34, in <module>
File "site-packages\lightgbm\basic.py", line 29, in _load_lib
File "ctypes\__init__.py", line 426, in LoadLibrary
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 151, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\...\\AppData\\Local\\Temp\\_MEI7562\\lightgbm\\../compile\\lib_lightgbm.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[4060] Failed to execute script pro
Can anyone help me resolve this issue?

Categories