Skip to content

Jinja2 error when trying to render a Pandas DataFrame to html from within a PyInstaller distribution #38775

@boazdori

Description

@boazdori
  • I have checked that this issue has not already been reported.
    Yes
  • I have confirmed this bug exists on the latest version of pandas.
    Yes
  • (optional) I have confirmed this bug exists on the master branch of pandas.
    Yes

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

pandas-io-formats-style.py line 133
loader = jinja2.PackageLoader("pandas", "io/formats/templates")
env = jinja2.Environment(loader=loader, trim_blocks=True)
template = env.get_template("html.tpl")

# Your code here
replaced the above lines with the following
import sys

if getattr(sys, 'frozen', False):
        # we are running in a bundle
        bundle_dir = sys._MEIPASS
        loader = jinja2.FileSystemLoader(bundle_dir)
else:
        loader = jinja2.PackageLoader("pandas", "io/formats/templates")
        
    env = jinja2.Environment(loader=loader, trim_blocks=True)
    template = env.get_template("html.tpl")


#### Problem description
When trying to package projects which include pandas and jinja2 with pyinstaller and running the produced exe we are getting the following error:
File "lib\site-packages\pandas\io\formats\style.py", line 62, in <module>
File "lib\site-packages\pandas\io\formats\style.py", line 135, in Styler
File "lib\site-packages\jinja2\environment.py", line 883, in get_template
File "lib\site-packages\jinja2\environment.py", line 857, in _load_template
File "lib\site-packages\jinja2\loaders.py", line 115, in load
File "lib\site-packages\jinja2\loaders.py", line 248, in get_source
jinja2.exceptions.TemplateNotFound: html.tpl

#### Expected Output
To run after compilation
#### Notes
If the code is replaced in pandas-io-formats-style.py line 133 as suggested above the problem is resolved. 
See StackOverflow link below
[https://stackoverflow.com/questions/31259673/unable-to-include-jinja2-template-to-pyinstaller-distribution](URL)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions