Search code examples
pythondataframepyinstallerpython-3.7

How to solve "jinja2.exceptions.TemplateNotFound: html.tpl" with excutable python created by Pyinstaller


I created an exe for my script using Pyinstaller and while running the exe it throws the following error where as if I run the .py file no issues found.

Traceback (most recent call last):
  File "myApplication.py", line 12, 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 "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "dataframe_image\__init__.py", line 2, in <module>
    """This directory is meant for IPython extensions."""
  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 "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "dataframe_image\_pandas_accessor.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 "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "pandas\io\formats\style.py", line 64, in <module>
  File "pandas\io\formats\style.py", line 143, in Styler
  File "jinja2\environment.py", line 883, in get_template
  File "jinja2\environment.py", line 857, in _load_template
  File "jinja2\loaders.py", line 115, in load
  File "jinja2\loaders.py", line 249, in get_source
jinja2.exceptions.TemplateNotFound: html.tpl
[18152] Failed to execute script myApplication

Here, [File "myApplication.py", line 12, in ] is "import dataframe_image as dfi" in my script.

I don't use jinja2 module directly in my script.

I tried the build option, --hidden-import=dataframe_image, it doesn't work for this issue.

Pyinstaller=4.3 Python=3.7.7

Thank you for your help in advance!

[updates on 21th Apr.]

The solution, pyinstaller --add-data path-to-site-packages\pandas\io\formats\templates\html.tpl;pandas\io\formats\templates solved above error but another error arose.

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1705, in __call__
  File "myApplication.py", line 374, in main
  File "dataframe_image\_pandas_accessor.py", line 24, in export
  File "dataframe_image\_pandas_accessor.py", line 33, in _export
  File "dataframe_image\_screenshot.py", line 79, in __init__
  File "dataframe_image\_screenshot.py", line 86, in get_css
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\***\\AppData\\Local\\Temp\\_MEI252882\\dataframe_image\\static\\style.css'

Here, File "myApplication.py", line 374, in main is dataframe_image.export(style, 'myTable.png')


Solution

  • This is problem with how pandas templates are included with the bundle. There are two issues reported with pandas dev team:

    and one reported with pyinstaller team:

    Check this answer from pyinstaller team: https://github.com/pyinstaller/pyinstaller/issues/5360#issuecomment-737197845

    If it doesn't work for you check this answer to resolve the issue:

    It's not the best, because requires to edit pandas code, but anyway.