gpt4 book ai didi

python - Pyinstaller Jinja2 TemplateNotFound

转载 作者:太空狗 更新时间:2023-10-29 20:53:39 25 4
gpt4 key购买 nike

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

它给了我 jinja2.exceptions.TemplateNotFound,

我尝试将 from app import template 放在模板文件夹中,但它没有用(我猜是因为它们不包含任何 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文件

# -*- 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因为它解决了问题。

编辑3

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

最佳答案

我不认为问题出在 https://stackoverflow.com/a/35816876/2741329 中描述的内容.我刚刚能够使用 Jinja2 卡住应用程序。

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

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。

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


编辑

这可能有效(假设您有一个包(即,您有一个 __init__.py)遵循这些建议: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')

关于python - Pyinstaller Jinja2 TemplateNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com