找不到Pyinstaller Jinja2模板

2024-10-01 11:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyinstaller来构建我的flask应用程序, 一切都很好,除了我在Jinja2模板上遇到问题。在

它给了我jinja2.exceptions.TemplateNotFound

我试图将from app import template放在templates文件夹中,但是没有用(我想是因为它们不包含任何py文件)。在

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

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)

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

有没有办法把模板和.exe捆绑在一起?在


编辑

这是我的spec文件

^{pr2}$

编辑2

接受的答案更改为^{},因为它修复了问题。在

编辑3

我还意识到,我可以用我的包名创建一个新的文件夹,填充静态模板csshtml,等等,它就可以工作了(类似于gmas80脚本的结果)


Tags: 文件py文件夹模板falseapp编辑added
2条回答

我不相信这个问题是https://stackoverflow.com/a/35816876/2741329中所描述的。我刚刚能够用Jinja2冻结一个应用程序。在

在我的spec文件中,我使用以下方法收集所有模板:

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

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


编辑

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

^{pr2}$

^{}包使用了PyInstaller不支持的^{}API。pkg_resources模块是通过^{}包提供的。在

^{}页,共^{}

pkg_resources is currently not supported by PyInstaller. This means that an application using a library which uses the the pkg_resources API will probably not work out of the box. The only situation in which it works is when it's being used on .egg files (see above). For details follow issue #183.

相关问题 更多 >