WinError Converting python script to executable with asyncio lib and pyinstaller - python

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!

Related

Error in geopandas module while running binary application of python script in linux

When I run python script it is working fine but when tried to run executable of same python script made using pyinstaller then getting module error. Specifically getting error while importing geopanda module. The error coming is mentioned below:
ERROR 1: PROJ: proj_create_from_database: Cannot find proj.db
PROJ: proj_create_from_database: Cannot find proj.db
Traceback (most recent call last):
File "ImageChecker.py", line 27, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
File "geopandas/__init__.py", line 17, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
File "geopandas/datasets/__init__.py", line 6, in <module>
StopIteration
[7062] Failed to execute script 'ImageChecker' due to unhandled exception!

Problems with Converting a .py to .exe

I am trying to convert a .py to a .exe with auto-py-to-exe.
I get this error when i run the .exe
Traceback (most recent call last):
File "main.py", line 4, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "random_word\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "random_word\random_word.py", line 20, in <module>
File "random_word\utils\utils.py", line 28, in get_api_keys
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\phili\\AppData\\Local\\Temp\\_MEI162522\\random_word\\config.yml'
[15844] Failed to execute script main
Since the error you are getting is FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\phili\\AppData\\Local\\Temp\\_MEI162522\\random_word\\config.yml' I am assuming that inside of your python script you hardcoded the filepath to config.yml. Once you compile your python script and move it to a different directory, the script will not be able to find the file anymore and yield the FileNotFoundError.
To fix the issue you can either provide a command line flag that provides the filepath to config.yml or you can embedd the file into your python script as described in this post.
Fixed by using another way to generate random words.

The 'google-api-python-client' distribution was not found on running EXE compiled by pyinstaller

My application using google calendar API with the Google Python client library
from googleapiclient.discovery import build
for
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=100, singleEvents=True,
orderBy='startTime').execute()
Create EXE using py installer
Versions -
72 INFO: PyInstaller: 4.1.dev0
72 INFO: Python: 3.9.0
74 INFO: Platform: Windows-10-10.0.18362-SP0
79 INFO: wrote C:\Users\PC\AppData\Local\Programs\Python\Python39\Scripts\main.spec
82 INFO: UPX is not available.
83 INFO: Extending PYTHONPATH with paths
Error
Traceback (most recent call last):
File "main.py", line 10, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "googleapiclient\discovery.py", line 68, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "googleapiclient\http.py", line 67, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "googleapiclient\model.py", line 36, in <module>
File "pkg_resources\__init__.py", line 480, in get_distribution
File "pkg_resources\__init__.py", line 356, in get_provider
File "pkg_resources\__init__.py", line 899, in require
File "pkg_resources\__init__.py", line 785, in resolve
pkg_resources.DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application
[15548] Failed to execute script main
line 10 -
from googleapiclient.discovery import build
The application working in pycharm and as python script in CMD
I used py installer in CMD
PyInstaller -F C:\Users\PC\PycharmProjects\.....
in script pycharm
import PyInstaller.__main__
PyInstaller.__main__.run([
'main.py',
'--onefile',
'--windowed'
])
Im following questions about this but did not find the answer
Thanks
The solution
copy the folder-- google_api_core-1.23.0-py3.8.egg-info-- to execute directory
rename folder name to ---google_api_python_client.egg-info--
On Windows, Copy Google directories from your Python Application install locations Python//Lib/site-packages directory to the dist/<dot_py_file> directory created by pyinstaller.

Getting error when converting python to exe (because of folium library)

Here is my Python code, which simply generates a map into a .html file using Folium module. As expected, the code doesn't return any error.
import folium
m = folium.Map(location=[39.3999, 8.2245], tiles='cartodbpositron', zoom_start=3)
m.save('output.html')
However when I convert the python code to .exe file, I get the following error when I want to execute the .exe file:
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 "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "folium/__init__.py", line 5, 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 "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "branca/__init__.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 "/home/nade/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "branca/colormap.py", line 20, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIS1K8Tv/branca/_cnames.json'
[17356] Failed to execute script test
I have used Pyinstaller to convert the python to .exe, here is the command I used (I used auto-py-to-exe for more specific convertion):
pyinstaller --noconfirm --onefile --windowed --icon "/path/to/icon" --key "encryption-key-value" "/path/to/script"
I have realised that this happens only because of Folium library. How do I make this module compatible when converting to .exe?
I did some research and found a workaround for this issue.
The solution based on this site: https://www.tutorialfor.com/questions-153381.htm
First, you need to modify these 3 files:
\folium\folium.py
\folium\raster_layers.py
\branca\element.py
Change the line ENV = Environment(loader=PackageLoader('folium', 'templates')) to below both 3 files.
#ENV = Environment(loader=PackageLoader('folium', 'templates'))
import os, sys
from jinja2 import FileSystemLoader
if getattr(sys, 'frozen', False):
# we are running in a bundle
templatedir = sys._MEIPASS
else:
# we are running in a normal Python environment
templatedir = os.path.dirname(os.path.abspath(__file__))
ENV = Environment(loader=FileSystemLoader(templatedir + '\\templates'))
After that, you need to edit the .spec file. Open the spec file and add datas section the below lines.
...
binaries=[],
datas=[
(".\\env\\Lib\\site-packages\\branca", "branca"),
(".\\env\\Lib\\site-packages\\branca\\*.json","branca"),
(".\\env\\Lib\\site-packages\\branca\\templates", "templates"),
(".\\env\\Lib\\site-packages\\folium\\templates", "templates"),
],
hiddenimports=[],
...
Then build using spec file.
pyinstaller <script_name>.spec

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

Categories