Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

build python script to single exe with pyinstaller

I am getting below errors : script name = prepareIncidentCountMail.py

Traceback (most recent call last):
  File "Alexa\prepareIncidentCountMail.py", line 52, in <module>
  File "site-packages\pandas\core\frame.py", line 683, in style
  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\avikumar\documents\learn\alexa\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pandas\io\formats\style.py", line 50, in <module>
  File "site-packages\pandas\io\formats\style.py", line 118, in Styler
  File "site-packages\jinja2\environment.py", line 830, in get_template
  File "site-packages\jinja2\environment.py", line 804, in _load_template
  File "site-packages\jinja2\loaders.py", line 113, in load
  File "site-packages\jinja2\loaders.py", line 234, in get_source
  File "site-packages\pkg_resources\__init__.py", line 1459, in has_resource
  File "site-packages\pkg_resources\__init__.py", line 1509, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[10536] Failed to execute script prepareIncidentCountMail

I am using pandas style with the help of link : Change color of complete row in data frame in pandas

I see style is using jinja2 causing the above error. Is there any way to hook this error or any other tool to convert python script into single executable.

like image 837
avinashse Avatar asked Apr 10 '18 12:04

avinashse


People also ask

Can you convert Python script to exe?

In order to more easily run Python scripts on Windows machines, you can convert Python to .exe files using either cx_freeze or PyInstaller.

Does PyInstaller compile Python?

The bundled app does not include any source code. However, PyInstaller bundles compiled Python scripts ( . pyc files).

Which is better py2exe or PyInstaller?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.


1 Answers

I just solved this myself yesterday using a tweaked version of what giumas did here: https://github.com/pyinstaller/pyinstaller/issues/1898

The issue isn't so much hooking (which was my first attempt at a solution), but the fact that pandas style module imports jinja2 which uses a "get_template" method which in turn uses the pkg_resources module. That last one is the issue, for some reason pyinstaller doesn't play well with the pkg_resources module.

Solution: Find where pandas is installed and go to something like

C:\Users\UserName\AppData\Local\Programs\Python\Python36\Lib\site-packages\pandas\io\formats

In the formats folder find the style.py file and open it in your favorite text editor. In style.py scroll down to about line 118 where you will see this:

template = env.get_template("html.tpl")

change this line to:

template = env.from_string("html.tpl")

save the file and re-run pyinstaller. When you try and run the executable it should perform as expected sans any error messages.

Hope it helps.

like image 74
Nick Wantz Avatar answered Sep 24 '22 22:09

Nick Wantz