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

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

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!

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.

How to fix "OSError: cannot open profile file" using Pyinstaller

I am working on a small script, which is going to open external .PSD and do some logging from it.
I am using psd_tools to work with PSD. It uses PIL.
Everything works fine until I bundle it into .EXE with pyinstaller.
Then it throws OSError: cannot open profile file.
I cant figure out whether the problem is in PIL, psd_tools or pyinstaller(Well, I am pretty newbie though).
Here is the code:
from psd_tools import *
psd = PSDImage.load('C:\python3\layers.psd')
print('success')
Here is the traceback:
Traceback (most recent call last):
File "layers_tr.py", line 1, in <module>
from psd_tools import *
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\Alexander\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\psd_tools\__init__.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\Alexander\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\psd_tools\user_api\psd_image.py", line 10, 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\Alexander\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\psd_tools\user_api\pil_support.py", line 9, 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\Alexander\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\psd_tools\icc_profiles\__init__.py", line 10, in <module>
File "site-packages\PIL\ImageCms.py", line 162, in __init__
OSError: cannot open profile file
[12424] Failed to execute script layers_tr
Any help pls
Updaing psd_tools with
pip install -U psd_tools
solved the problem. Big Thanks to M. R.

Cx_Freeze 5.0.1 + Python3.4 virtualenv error

I have been trying to figure out what the following error means but unfortunately I am not being able to handle it. The complete traceback resulting from the execution of the generated executable is:
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "/home/prog3/Ambiente/p34/lib/python3.4/site-packages/cx_Freeze-5.0.1-py3.4-linux-x86_64.egg/cx_Freeze/initscripts/__startup__.py", line 12, in <module>
__import__(name + "__init__")
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "/home/prog3/Ambiente/p34/lib/python3.4/site-packages/cx_Freeze-5.0.1-py3.4-linux-x86_64.egg/cx_Freeze/initscripts/Console.py", line 24, in <module>
exec(code, m.__dict__)
File "App.py", line 10, in <module>
File "/home/prog3/Ambiente/p34/lib/python3.4/site-packages/glymur/__init__.py", line 4, in <module>
from glymur import version
File "/home/prog3/Ambiente/p34/lib/python3.4/site-packages/glymur/version.py", line 15, in <module>
from distutils.version import LooseVersion
File "/home/prog3/Ambiente/p34/lib/python3.4/distutils/__init__.py", line 17, in <module>
real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ('', '', imp.PKG_DIRECTORY))
File "/home/prog3/Ambiente/p34/lib/python3.4/imp.py", line 245, in load_module
return load_package(name, filename)
File "/home/prog3/Ambiente/p34/lib/python3.4/imp.py", line 217, in load_package
return methods.load()
File "<frozen importlib._bootstrap>", line 1219, in load
AttributeError: 'NoneType' object has no attribute 'name'
My setup.py file looks like:
import sys
from cx_Freeze import setup,Executable
includefiles = ['libopenjp2.so','libopenjp2.so.2.2.0','libopenjp2.so.7']
includes = []
excludes = ['Tkinter']
packages = ["os","sys","PyQt5.QtCore","PyQt5.QtGui","PyQt5.QtWidgets","numpy", "subprocess","encodings","glymur","matplotlib","math","PIL","skimage","scipy","matplotlib.backends.backend_qt5agg","lxml","ctypes","re","collections","struct","contextlib","itertools","uuid","warnings"]
build_exe_options = {"packages":[],
"excludes":["Tkinter"],"includefiles":['libopenjp2.so','libopenjp2.so.2.2.0','libopenjp2.so.7'],"includes":[],"copy_dependent_files":True
}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(name='alignosis',
version='0.1',
description='Alignment of two differently stained whole-slide JPEG2000 digital pathology images',
options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables= [Executable('App.py')])
I must say that once my virtualenv containing all the required packages is activated and after execution of the above setup.py by: python setup.py build, I get no errors and so it is created the build folder.
I am aware that probably I dont need to have a so big list of packages to include given that some of them are built-in and I am sorry for that...
I appreciate any suggestion/help that you can give to me.
Thank you

Categories