在烧瓶应用程序上使用 cx_freeze

Posted

技术标签:

【中文标题】在烧瓶应用程序上使用 cx_freeze【英文标题】:using cx_freeze on flask app 【发布时间】:2012-12-26 13:44:31 【问题描述】:

我正在使用 Flask 开发一个 python 应用程序。目前,我希望这个应用程序在本地运行。它通过 python 在本地运行良好,但是当我使用 cx_freeze 将其转换为 Windows 的 exe 时,我不能再使用 Flask.render_template() 方法。在我尝试执行 render_template 的那一刻,我得到一个 http 500 错误,就像我尝试渲染的 html 模板不存在一样。

主要的python文件称为index.py。起初我尝试运行:cxfreeze index.py。这不包括 cxfreeze“dist”目录中 Flask 项目的“templates”目录。然后我尝试使用这个 setup.py 脚本并运行python setup.py build。这现在包括模板文件夹和 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 -

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

在我的浏览器中。

如果我返回原始 html,例如

@app.route('/index', methods=['GET'])
def index():
    print "rendering index"
    return "This works"

然后它工作正常。因此,一种可能的解决方法是停止使用 Flask 的模板并将所有 html 逻辑硬编码到主 python 文件中。不过这会变得非常混乱,所以我想尽可能避免它。

我正在使用 Python 2.7 32 位、Cx_freeze 用于 Python 2.7 32 位和 Flask 0.9

感谢您的帮助和想法!

【问题讨论】:

【参考方案1】:

在 Flask 和 Jinga 模块中搜索了许多虚假线索后,我终于找到了问题。

CXFreeze 无法识别 jinja2.ext 是一个依赖项,并且没有包含它。

我通过在其中一个 python 文件中包含 import jinja2.ext 来解决此问题。

CXFreeze 然后将ext.pyc 添加到 library.zip\jinja。 (构建后手动复制也可以)

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

【讨论】:

【参考方案2】:

源文件中import 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')]
)

【讨论】:

以上是关于在烧瓶应用程序上使用 cx_freeze的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 gunicorn 上运行的烧瓶应用程序中使用日志轮换时同时在多个文件上写入日志?

使用 cx_freeze 在 Mac、Linux 和 Windows 上分发 python:我可以从一个平台生成所有应用程序吗?

在 Windows 上使用 apache mod_wsgi 运行烧瓶应用程序时的导入冲突

使用 pyaudio 在 linux/ubuntu 上运行烧瓶应用程序时出现 ALSA 错误

在 Uwsgi 和 Nginx 上使用 SocketIO 烧瓶

如何确保烧瓶应用程序已在所有路线上获得授权?