使用pyinstaller构建的Flask应用程序未呈现index.html [英] Flask application built using pyinstaller not rendering index.html

查看:605
本文介绍了使用pyinstaller构建的Flask应用程序未呈现index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个flask应用程序,它运行良好.我想将其分发为可执行文件. 尝试使用pyinstaller flaskScript.py进行此操作 dist文件夹已生成. 进入dist文件夹并双击我的可执行flaskScript,它将启动我的服务器. 在访问url:localhost:9090时,它会给出以下异常

I have written a flask application and it works perfectly fine. I wanted to distribute it as an executable. Tried doing it using pyinstaller flaskScript.py dist folder got generated. Went into the dist folder and double clicked my executable flaskScript, it starts my server. On accessing the url, localhost:9090 it gives the following exception

jinja2.exceptions.TemplateNotFound

TemplateNotFound: index.html

Traceback (most recent call last)
File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1836, in __call__

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1820, in wsgi_app

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1403, in handle_exception

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1817, in wsgi_app

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1477, in full_dispatch_request

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1381, in handle_user_exception

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1475, in full_dispatch_request

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.app", line 1461, in dispatch_request

File "<string>", line 13, in index

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.templating", line 127, in render_template

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/jinja2.environment", line 851, in get_or_select_template

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/jinja2.environment", line 812, in get_template

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/jinja2.environment", line 774, in _load_template

File "/Users/george/Downloads/flaskApps/flaskScript/build/flaskScript/out00-PYZ.pyz/flask.templating", line 64, in get_source

TemplateNotFound: index.html

虽然在执行python flaskScript.py的同时在开发设置中可以正常工作

While it works fine in the dev setup while executing python flaskScript.py

推荐答案

这个问题现在有点老了,但是打包时我遇到了同样的问题(Python 3.4.3,Flask 0.10.1和PyInstaller 3.1.1)它保存到一个文件中.

This question is a bit old now, but I was having the same problem (Python 3.4.3, Flask 0.10.1 and PyInstaller 3.1.1) when packaging it to a single file.

我设法通过将以下内容添加到初始化脚本(app\__init__.py)中来解决该问题:

I've managed to solve it by adding the following to the initialization script (app\__init__.py):

import sys
import os
from flask import Flask
# Other imports

if getattr(sys, 'frozen', False):
    template_folder = os.path.join(sys._MEIPASS, 'templates')
    app = Flask(__name__, template_folder=template_folder)
else:
    app = Flask(__name__)

# etc

问题在于,当网站以打包形式运行时,模板位于temp目录下的一个名为 _MEIxxxxxx 的目录中(请参阅

The problem is that when the site is running in packaged form, the templates are inside a directory called _MEIxxxxxx under the temp directory (see this in the PyInstaller Manual) so we have to tell that to Flask.

这是通过template_folder参数完成的(我在此答案中在此处以及随后的内容中找到了此参数) API文档).

That is done with the template_folder argument (which I found out about in this answer here and later in the API docs).

最后,在这里if可以确保我们在开发它时仍可以不打包使用它.如果它是frozen,则将脚本打包,并且我们必须告诉Flask在哪里可以找到模板;否则,我们将在Python环境中运行它(取自

Finally, the if is there to ensure that we can still use it unpackaged while developing it. If it is frozen, then the script is packaged and we have to tell Flask where to find the templates; otherwise we're running it in a Python environment (taken from here) and the defaults will work (assuming you're using the standard templates directory of course).

这篇关于使用pyinstaller构建的Flask应用程序未呈现index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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