Python 使用cx_冻结烧瓶应用程序

Python 使用cx_冻结烧瓶应用程序,python,flask,cx-freeze,Python,Flask,Cx Freeze,我正在使用Flask开发一个python应用程序。目前,我希望此应用程序在本地运行。它通过python在本地运行良好,但是当我使用cx\u freeze将其转换为Windows的exe时,我就不能再使用Flask.render\u template()方法了。在我尝试执行render_模板的那一刻,我得到了一个http 500错误,就好像我试图呈现的html模板不存在一样 主python文件名为index.py。起初我尝试运行:cxfreeze index.py。这不包括cxfreeze“dis

我正在使用Flask开发一个python应用程序。目前,我希望此应用程序在本地运行。它通过python在本地运行良好,但是当我使用cx\u freeze将其转换为Windows的exe时,我就不能再使用Flask.render\u template()方法了。在我尝试执行render_模板的那一刻,我得到了一个http 500错误,就好像我试图呈现的html模板不存在一样

主python文件名为index.py。起初我尝试运行:
cxfreeze index.py
。这不包括cxfreeze“dist”目录中Flask项目的“templates”目录。然后我尝试使用这个setup.py脚本并运行
python setup.py build
。这现在包括templates文件夹和index.html模板,但当它试图呈现模板时,仍然会出现http:500错误

from cx_Freeze import setup,Executable

includefiles = [ 'templates\index.html']
includes = []
excludes = ['Tkinter']

setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = 'me@me.com',
options = {'build_exe': {'excludes':excludes,'include_files':includefiles}}, 
executables = [Executable('index.py')]
)
以下是脚本中的示例方法:

@app.route('/index', methods=['GET'])
def index():
    print "rendering index"
    return render_template("index.html")
如果我运行
index.py
,那么在控制台中我会得到:

 * Running on http://0.0.0.0:5000/
 rendering index
 127.0.0.1 - - [26/Dec/2012 15:26:41] "GET / HTTP/1.1" 200 -
 127.0.0.1 - - [26/Dec/2012 15:26:42] "GET /favicon.ico HTTP/1.1" 404 -
并且页面在我的浏览器中正确显示,但是如果我运行
index.exe
,我会

 * Running on http://0.0.0.0:5000/
rendering index
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET /favicon.ico HTTP/1.1" 404 -

在我的浏览器中

如果我返回原始html,例如

@app.route('/index', methods=['GET'])
def index():
    print "rendering index"
    return "This works"
那么它工作得很好。因此,一个可能的解决办法是停止使用Flask的模板,并将所有html逻辑硬编码到主python文件中。这会变得非常混乱,所以如果可能的话,我想避免它

我使用的是Python 2.7 32位、Cx_freeze for Python 2.7 32位和Flask 0.9


谢谢你的帮助和想法

在烧瓶和Jinga模块中拖网搜寻了许多错误线索后,我终于找到了问题所在

CXFreeze不承认jinja2.ext是一个依赖项,并且没有包括它

我通过在一个python文件中包含
import jinja2.ext
修复了这个问题

CXFreeze然后将
ext.pyc
添加到library.zip\jinja。(生成后手动将其复制进来也可以正常工作)


以防其他人疯狂地尝试使用Flask开发本地运行的应用程序:)

在源文件中导入jinja2.ext的替代方法是在setup.py中特别包括
jinja2.ext

from cx_Freeze import setup,Executable

includefiles = [ 'templates\index.html']
includes = ['jinja2.ext']  # add jinja2.ext here
excludes = ['Tkinter']

setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = 'me@me.com',
# Add includes to the options
options = {'build_exe':   {'excludes':excludes,'include_files':includefiles, 'includes':includes}},   
executables = [Executable('index.py')]
)
from cx_Freeze import setup,Executable

includefiles = [ 'templates\index.html']
includes = ['jinja2.ext']  # add jinja2.ext here
excludes = ['Tkinter']

setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = 'me@me.com',
# Add includes to the options
options = {'build_exe':   {'excludes':excludes,'include_files':includefiles, 'includes':includes}},   
executables = [Executable('index.py')]
)