Pyinstaller Jinja2 TemplateNotFound [英] Pyinstaller Jinja2 TemplateNotFound

查看:95
本文介绍了Pyinstaller Jinja2 TemplateNotFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pyinstaller来构建我的flask应用程序, 一切正常,除了Jinja2模板出现问题.

I am using pyinstaller to build my flask application, everything is working fine except I get problems with Jinja2 templates.

它给了我jinja2.exceptions.TemplateNotFound

我试图放入from app import template这是模板文件夹,但是没有用(我想是因为它们不包含任何py文件).

I tried to put from app import template which is the templates folder, but it didn't work (I guess since they don't contain any py file).

我还尝试将.spec文件更改为包含templates文件夹

I also tried changing the .spec file to include the templates folder

added_files = [
         ( '..\\CommerceApp\\app\\templates', 'templates' ),
         ( '..\\CommerceApp\\app\\static', 'static' )
        ]

a = Analysis(['..\\CommerceApp\\run.py'],
             pathex=['D:\\PythonProjects\\CommerceAppExe'],
             binaries=None,
             datas=added_files,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

但是它也不起作用,就像我自己手动复制文件夹一样.

But it didn't work either, same result as if I copy the folder manually by myself.

是否有任何方法可以包含与.exe捆绑在一起的模板?

Is there any way to include Template bundled together with the .exe?

修改

这是我的spec文件

# -*- mode: python -*-

block_cipher = None

a = Analysis(['..\\CommerceApp_withPyInstaller\\run.py'],
             pathex=['D:\\PythonProjects\\CommerceAppExe'],
             binaries=None,
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='SupplyTracker',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='SupplyTracker')


编辑2

接受的答案"更改为 gmas80 ,因为它可以解决问题.

Accepted Answer changed to gmas80 because it fixes the problem.

编辑3

我还意识到,我可以使用包名称创建一个新文件夹并填写静态模板csshtml等,并且它将起作用(与gmas80脚本的结果类似)

Also I just realize, I can just make a new folder with my package name and fill in the static templates css, html, etc, and it is gonna work (similar result from what gmas80 script does)

推荐答案

我不认为问题在于 https://stackoverflow.com/a/35816876/2741329 .我刚刚能够使用Jinja2冻结应用程序.

I don't believe that the issue is what is described in https://stackoverflow.com/a/35816876/2741329. I have just been able to freeze an application with Jinja2.

在我的规格文件中,我使用这种方法来收集所有模板:

In my spec file I use this approach to collect all the templates:

from PyInstaller.building.build_main import Analysis, PYZ, EXE, COLLECT, BUNDLE, TOC


def collect_pkg_data(package, include_py_files=False, subdir=None):
    import os
    from PyInstaller.utils.hooks import get_package_paths, remove_prefix, PY_IGNORE_EXTENSIONS

    # Accept only strings as packages.
    if type(package) is not str:
        raise ValueError

    pkg_base, pkg_dir = get_package_paths(package)
    if subdir:
        pkg_dir = os.path.join(pkg_dir, subdir)
    # Walk through all file in the given package, looking for data files.
    data_toc = TOC()
    for dir_path, dir_names, files in os.walk(pkg_dir):
        for f in files:
            extension = os.path.splitext(f)[1]
            if include_py_files or (extension not in PY_IGNORE_EXTENSIONS):
                source_file = os.path.join(dir_path, f)
                dest_folder = remove_prefix(dir_path, os.path.dirname(pkg_base) + os.sep)
                dest_file = os.path.join(dest_folder, f)
                data_toc.append((dest_file, source_file, 'DATA'))

    return data_toc

pkg_data = collect_pkg_data('<YOUR LIB HERE>')

然后将pkg_data添加到COLLECT(1个文件夹)或EXE(1个文件).spec.

Then add pkg_data to the COLLECT (1-folder) or to the EXE (1-file) .spec.

在1文件夹解决方案中,您应该能够在创建的子文件夹中找到所有模板.

In the 1-folder solution, you should be able to find all your templates in the created sub-folder.

修改

这可能会起作用(假设您有一个软件包(即,您有一个__init__.py)),请遵循以下建议: http://flask.pocoo.org/docs/0.10/patterns/packages/):

This might work (assuming that you have a package (i.e., you have an __init__.py) following these suggestions: http://flask.pocoo.org/docs/0.10/patterns/packages/):

# -*- mode: python -*-

# <<< START ADDED PART    
from PyInstaller.building.build_main import Analysis, PYZ, EXE, COLLECT, BUNDLE, TOC


def collect_pkg_data(package, include_py_files=False, subdir=None):
    import os
    from PyInstaller.utils.hooks import get_package_paths, remove_prefix, PY_IGNORE_EXTENSIONS

    # Accept only strings as packages.
    if type(package) is not str:
        raise ValueError

    pkg_base, pkg_dir = get_package_paths(package)
    if subdir:
        pkg_dir = os.path.join(pkg_dir, subdir)
    # Walk through all file in the given package, looking for data files.
    data_toc = TOC()
    for dir_path, dir_names, files in os.walk(pkg_dir):
        for f in files:
            extension = os.path.splitext(f)[1]
            if include_py_files or (extension not in PY_IGNORE_EXTENSIONS):
                source_file = os.path.join(dir_path, f)
                dest_folder = remove_prefix(dir_path, os.path.dirname(pkg_base) + os.sep)
                dest_file = os.path.join(dest_folder, f)
                data_toc.append((dest_file, source_file, 'DATA'))

    return data_toc

pkg_data = collect_pkg_data('<yourapplication>')  # <<< Put the name of your package here
# <<< END ADDED PART    

block_cipher = None

a = Analysis(['..\\CommerceApp_withPyInstaller\\run.py'],
             pathex=['D:\\PythonProjects\\CommerceAppExe'],
             binaries=None,
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='SupplyTracker',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               pkg_data,  # <<< Add here the collected files
               strip=False,
               upx=True,
               name='SupplyTracker')

这篇关于Pyinstaller Jinja2 TemplateNotFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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