如何使用 Pyinstaller 创建的可执行文件 python 解决 "jinja2.exceptions.TemplateNotFound: html.tpl"

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

我使用 Pyinstaller 为我的脚本创建了一个 exe,当 运行 运行 exe 时它抛出以下错误,好像我 运行 .py 文件没有发现问题。

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

这里,[File "myApplication.py", line 12, in ] 在我的脚本中是“import dataframe_image as dfi”。

我没有在我的脚本中直接使用 jinja2 模块。

我尝试了构建选项 --hidden-import=dataframe_image,它对这个问题不起作用。

Pyinstaller=4.3 Python=3.7.7

提前感谢您的帮助!

[4 月 21 日更新]

解决方案,pyinstaller --add-data path-to-site-packages\pandas\io\formats\templates\html.tpl;pandas\io\formats\templates解决了上面的错误,但出现了另一个错误。

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'

这里,File "myApplication.py", line 374, in maindataframe_image.export(style, 'myTable.png')

这是 pandas 模板如何包含在捆绑包中的问题。 pandas 开发团队报告了两个问题:

其中一个报告给了 pyinstaller 团队:

查看 pyinstaller 团队的回答: https://github.com/pyinstaller/pyinstaller/issues/5360#issuecomment-737197845

如果它对您不起作用,请检查此答案以解决问题:

这不是最好的,因为需要编辑 pandas 代码,但无论如何。