Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: Missing optional dependency 'jinja2'. DataFrame.style requires jinja2. #5360

Closed
ahbon123 opened this issue Dec 2, 2020 · 7 comments

Comments

@ahbon123
Copy link

ahbon123 commented Dec 2, 2020

I've built a exe file using PyInstaller under Windows 10 OS, it successfully generates the exe file, but when I run it, it raise an error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1705, in __call__
  File "main_app.py", line 19, in open_directory
  File "data_check.py", line 273, in DataCheck
  File "pandas\core\frame.py", line 836, in style
  File "c:\users\72766\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pandas\io\formats\style.py", line 30, in <module>
  File "pandas\compat\_optional.py", line 92, in import_optional_dependency
ImportError: Missing optional dependency 'jinja2'. DataFrame.style requires jinja2. Use pip or conda to install jinja2.

Please help me to solve this issue. Thanks a lot.

@rokm
Copy link
Member

rokm commented Dec 2, 2020

Since jinja2 is optionally imported by pandas, pyinstaller does not collect it. So you either need to add jinja2 to hiddenimports (e.g., pyinstaller --hidden-import jinja2 ...) or explicitly import jinja2 somewhere in your program.


But after this, you will hit a new error: NotImplementedError: Can't perform this operation for unregistered loader type. This is due to missing support for pkg_resources provider (#4881), and should be fixed once #5284 is merged.

In the meantime, you can either:


And after loader issues are solved, you will get jinja2.exceptions.TemplateNotFound: html.tpl, which is due to the html.tpl template data file not being collected.

You can collect it directly using pyinstaller --add-data path-to-site-packages\pandas\io\formats\templates\html.tpl;pandas\io\formats\templates.

Or, you could use PyInstaller.utils.hooks.collect_data_files() in a custom hook or your .spec file to collect data files from pandas.io.formats (or even whole pandas). (There's an existing hook-pandas.py that comes with PyInstaller that you could extend to collect the data as well).

@ahbon123
Copy link
Author

ahbon123 commented Dec 2, 2020

Many thanks for your kind help. Sounds complicated, is it caused by the incompatibility of pandas and jinja2 or pyinstaller's bug?

@rokm
Copy link
Member

rokm commented Dec 2, 2020

The first part (having to add jinja2 to hiddenimports) is caused by the way pyinstaller works; the other two parts are due to current limitations of pyinstaller and its hooks.

Unfortunately, each python package has its own specifics, so sometimes it takes a bit of an effort to get things packed into an executable...

@ahbon123
Copy link
Author

ahbon123 commented Dec 3, 2020

Your answer perfectly solved my issue, thanks a lot.

@rokm rokm closed this as completed Dec 18, 2020
@lan2720
Copy link

lan2720 commented Mar 3, 2021

@ahbon123
I do as follows

  1. pyinstaller --hidden-import jinja2 ...
  2. pip install https://github.com/rokm/pyinstaller/archive/pkg-resources.zip
  3. modify pandas\io\formats\style.py to use jinja2.FileSystemLoader instead of jinja2.PackageLoader
    But it comes
    image
    Could you give some advice?

@bwoodsend
Copy link
Member

@lan2720 Thar could be anything. Get rid of the -w option to get a proper stacktrace.

@tsuce
Copy link

tsuce commented Mar 26, 2021

@lan2720 after step3, run the .py the html.tpl cannot be found. I tried to change code to
loader = jinja2.FileSystemLoader(searchpath="./")
put html.tpl to .py folder or .exe folder, it runs successfully.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants