我有一个使用Jinja2模板的python脚本,并尝试使用Pyinstaller创建一个单文件夹分发版。在
在Jinja中,我让程序通过使用PackageLoader
类来理解模板的位置。下面的代码显示它指向的是pycorr
Python包下的templates
文件夹。在
env = Environment(loader=PackageLoader('pycorr', 'templates'))
template = env.get_template('child_template.html')
下面是我的文件夹结构:
^{pr2}$当我使用Pyinstaller将包编译到一个文件夹中时,我没有看到任何与Jinja2相关的警告/错误,我可以启动.exe文件。但是,当程序开始查找Jinja2模板时,它将失败,控制台窗口上显示以下错误消息:
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文件中添加了以下行:
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')
但这似乎不能解决问题。在
有人能帮忙吗?我把Pyinstaller手册读了好几遍,但就是搞不懂。在
从@Uynix开始,我发现我必须执行更多的步骤来使用cx_freeze实现我版本的问题解决方案。我的第一个解决方案帖子,让我知道如果需要更多的细节。在
总之,我不得不修改 C: \ProgramData\Anaconda3\pkgs\bokeh-0.12.9-py36_0\Lib\site packages\bokeh\core\模板.py在
原始文件(bokeh 0.12.9):
我把问题追根溯源:
^{pr2}$我发现,由于某种原因,cx_freeze没有正确编译,抛出了相同的错误:
新的模板.py文件:
然后又运行了cx_freeze等,这一次bokeh现在工作了!在
我在尝试使用代码从PyInstaller发行版中将Pandas数据帧呈现为html时遇到了类似的Jinja2错误
我通过修改包加载器指令解决了这个问题。在
在Pandas样式文件中:site packages\Pandas\io\formats\样式.py在
我换了
^{pr2}$有了
文件顶部有相应的导入
现在,如果程序被冻结,那么加载程序将在bundle目录中查找模板。在这种情况下,最后一步是将模板添加到包中。为此,我使用adddata命令从命令行运行PyInstaller。例如,类似下面的命令将添加默认模板html.tpl文件你说
我在使用pyinstaller构建GUI时遇到了这个问题。我使用Jinja2呈现一个报表,模板没有加载,相反,我也收到了“unregistered loader type”错误。通过在线阅读和测试许多解决方案,我终于找到了一个解决方案:必须使用FileSystemLoader而不是PackageLoader。还需要为FileSystemLoader提供文件路径。我的最终解决方案是来自here和{a2}的信息的组合。在
下面提供了此解决方案的完整示例。我的代码在testjinjia2下,模板在子目录templates中:
在测试报告.spec公司名称:
^{pr2}$在测试报告.py你说
需要一个方法资源_路径,因为jinja模板文件的文件路径在我的IDE和从exe文件中提取的文件不同。在
还有一些简单的模板文件供您试用。
基本.html在
报表html
我使用的是pyinstaller 3.2.1和python3.5.1anaconda自定义(64位)
相关问题 更多 >
编程相关推荐