我已经写了一个flask应用程序,它工作得很好。我想把它作为可执行文件分发。尝试使用pyinstaller执行此操作时,生成了flaskScript.py dist文件夹。进入dist文件夹并双击我的可执行文件flaskScript,它将启动我的服务器。访问url时,localhost:9090会给出以下异常
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时在dev setup中工作得很好。
发布于 2016-04-07 22:03:59
这个问题现在有点老了,但我在将其打包到单个文件时也遇到了同样的问题(Python3.4.3,Flask 0.10.1和PyInstaller 3.1.1)。
我已经设法通过在初始化脚本(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的目录中(请参阅Flask手册中的this ),所以我们必须告诉PyInstaller。
这是通过template_folder参数完成的(我在这个答案here和后面的API docs中都了解到了这个参数)。
最后,if的存在是为了确保我们在开发它时仍然可以不打包地使用它。如果它是frozen,那么脚本已经打包,我们必须告诉Flask在哪里可以找到模板;否则,我们将在Python环境中运行它(取自here),并且默认设置将有效(当然,假设您使用的是标准templates目录)。
发布于 2018-05-02 18:47:20
如果您正在尝试创建一个--onefile可执行文件,那么您还需要在spec文件中添加目录。
base_dir中:导入操作系统,sys base_dir =‘’if hasattr(sys,'_MEIPASS'):base_dir = os.path.join(sys._MEIPASS)
'templates'))
templates和static文件夹包括在pyinstaller的分析部分中:A=分析( ...datas= (‘应用程序的路径\静态\’,‘静态’),(‘应用程序的路径\模板\’,‘模板’),,...
稍微解释一下:
使用pyinstaller打包后找不到文件的一般问题是文件将更改其路径。使用--onefile选项,您的文件将在可执行文件中压缩。当您执行exe时,它们被解压并放在某个临时文件夹中。
每次执行文件时都会发生变化,但运行的应用程序(不是spec文件,而是您的主要python文件,比如main.py)可以在以下位置找到:
import os
os.path.join(sys._MEIPASS)所以,模板文件不是./templates格式的,而是os.path.join(os.path.join(sys._MEIPASS), templates)格式的。现在的问题是,除非打包,否则您的代码将无法运行。因此,python main.py不会运行。
因此,需要一个条件才能在正确的位置找到文件:
import os, sys
base_dir = '.'
if hasattr(sys, '_MEIPASS'): # or, untested: if getattr(sys, 'frozen', False):
base_dir = os.path.join(sys._MEIPASS)下面是关于runtime information in pyinstaller的更多信息
发布于 2017-03-28 13:22:16
if getattr(sys, 'frozen', False):
template_folder = os.path.join(sys.executable, '..','templates')
static_folder = os.path.join(sys.executable, '..','static')
app = Flask(__name__, template_folder = template_folder,\
static_folder = static_folder)复制此代码并粘贴到您的文件中。现在pyinstaller在dist目录中查找static和template文件夹。现在复制并粘贴dist文件夹中的static和template文件夹。它会起作用的
https://stackoverflow.com/questions/32149892
复制相似问题