无法将Jinja2模板包含到Pyinstaller分发中 [英] Unable to Include Jinja2 Template to Pyinstaller Distribution

查看:168
本文介绍了无法将Jinja2模板包含到Pyinstaller分发中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Jinja2模板的python脚本,并且我正在尝试使用Pyinstaller创建一个文件夹分布.

I have a python script that uses Jinja2 template, and I'm trying to create a one-folder distribution using Pyinstaller.

在Jinja中,我通过使用 PackageLoader 类让程序了解模板的位置.下面的代码表明它指向我的 pycorr Python包下的 templates 文件夹.

In Jinja, I'm letting the program understand the location of the templates by using a PackageLoader class. The code below shows that it's pointing to my templates folder under pycorr Python package.

env = Environment(loader=PackageLoader('pycorr', 'templates'))
template = env.get_template('child_template.html')

这是我的文件夹结构:

pycorr
| |
| + templates
|    |
|    + base.html
|    + child.html

当我使用Pyinstaller将程序包编译到一个文件夹中时,没有看到与Jinja2相关的任何警告/错误,并且我能够启动.exe文件.但是,当程序开始寻找Jinja2模板时,它将失败,并在控制台窗口上显示以下错误消息:

When I compile the package into a single folder using Pyinstaller, I don't see any warning/error related to Jinja2, and I'm able to start the .exe file. However when the program start to look for Jinja2 template, it fails with this error message displayed on the console window:

Traceback (most recent call last):
  ...
  File "C:\Users\ ... \out00-PYZ.pyz\pycorr.WriterToHTML", line 96, in htmlout_table
  File "C:\Users\ ... \out00-PYZ.pyz\pycorr.WriterToHTML", line 13, in __init__
  File "C:\Users\ ... \out00-PYZ.pyz\pycorr.WriterToHTML", line 48, in __set_template
  File "C:\Users\ ... \out00-PYZ.pyz\jinja2.environment", line 791, in get_template
  File "C:\Users\ ... \out00-PYZ.pyz\jinja2.environment", line 765, in _load_template
  File "C:\Users\ ... \out00-PYZ.pyz\jinja2.loaders", line 113, in load
  File "C:\Users\ ... \out00-PYZ.pyz\jinja2.loaders", line 224, in get_source
  File "C:\Users\ ... \dist\OCA_CO~1\eggs\setuptools-14.3-py2.7.egg\pkg_resources\__init__.py", line 1572, in has_resource 
    return self._has(self._fn(self.module_path, resource_name))
  File "C:\Users\ ... \dist\OCA_CO~1\eggs\setuptools-14.3-py2.7.egg\pkg_resources\__init__.py", line 1627, in _has
    "Can't perform this operation for unregistered loader type"
  NotImplementedError: Can't perform this operation for unregistered loader type

我不是很理解错误消息,但是我的猜测是Pyinstaller需要找到 templates 文件夹.因此,我在Pyinstaller .spec文件中添加了以下几行:

I don't really understand the error message, but my guess is that Pyinstaller need to find the templates folder. So I added these lines in the Pyinstaller .spec file:

a.datas += [('BASE', './pycorr/templates/base.html', 'DATA')]
a.datas += [('TABLE', './pycorr/templates/table_child.html', 'DATA')]
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=False,
               name='OCA_correlation')

但这似乎并不能解决问题.

But it doesn't seems to solve the issue.

任何人都可以帮忙吗?我已经多次阅读了Pyinstaller手册,但无法弄清.

Can anyone help? I read up the Pyinstaller manual several times but I just can't figure it out.

推荐答案

在尝试使用PyInstaller发行版中的代码将Pandas DataFrame呈现为html时,我遇到了类似的Jinja2错误,

I encountered a similar Jinja2 error when trying to render a Pandas DataFrame to html from within a PyInstaller distribution using the code,

html = df.style.render()

我通过修改软件包加载程序指令解决了该问题.

I resolved the issue by modifying the package loader instruction.

在Pandas样式文件中:site-packages \ pandas \ io \ formats \ style.py

In the Pandas style file: site-packages\pandas\io\formats\style.py

我替换了

loader = PackageLoader("pandas", "io/formats/templates")   

使用

if getattr(sys, 'frozen', False):
    # we are running in a bundle
    bundle_dir = sys._MEIPASS
    loader = FileSystemLoader(bundle_dir)
else:
    loader = PackageLoader("pandas", "io/formats/templates")

以及文件顶部的相应导入

And a corresponding import at the top of the file,

import sys

现在,如果程序被冻结",则加载程序将在bundle目录中查找模板.在这种情况下,最后一步是将模板添加到捆绑软件中.为此,我使用--add-data命令从命令行运行了PyInstaller.例如,类似于以下命令的命令将添加默认模板html.tpl,

Now, if the program is 'frozen' then the loader will look for the template in the bundle directory. In which case, the final step was to add the template to the bundle. To do this I ran PyInstaller from the command line with the --add-data command. For example, a command similar to that below adds the default template html.tpl,

pyinstaller --add-data PATH1\site-packages\pandas\io\formats\templates\html.tpl;.  PATH2\Main.py

这篇关于无法将Jinja2模板包含到Pyinstaller分发中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆