在 Flask 应用程序上使用 cx_freeze

问题描述 投票:0回答:3

我正在使用 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
。现在包括 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 = '[email protected]',
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 位、Python 2.7 32 位的 Cx_freeze 和 Flask 0.9

感谢您的帮助和想法!

python flask cx-freeze
3个回答
19
投票

在 Flask 和 Jinga 模块中进行了多次错误搜索后,我终于找到了问题所在。

CXFreeze 无法识别 jinja2.ext 是一个依赖项,因此未包含它。

我通过在其中一个 python 文件中包含

import jinja2.ext
来修复此问题。

CXFreeze 然后将

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

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


0
投票

源文件中

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 = '[email protected]',
# Add includes to the options
options = {'build_exe':   {'excludes':excludes,'include_files':includefiles, 'includes':includes}},   
executables = [Executable('index.py')]
)

0
投票

2023,让 Flask 在使用 cx_freeze 编译时看到 /templates/ 和 /static/ jinja2 文件夹

我刚刚开始工作,很高兴清理我的代码,有更好的方法来处理路径。不管怎样,这应该对你有帮助。

在我的 backend.py(烧瓶应用程序)中

from pathlib import Path

td = Path(__file__).parent.parent.parent / 'templates'
sd = Path(__file__).parent.parent.parent / 'static'

app = Flask(__name__, template_folder=td.resolve(), static_folder=sd.resolve())
app.config.from_pyfile

setup.py(python setup.py 构建)

    from cx_Freeze import setup, Executable

    import sys
    # Dependencies are automatically detected, but it might need
    # fine tuning.
    build_options = {'packages': [], 'excludes': []}

    base = 'console'

    executables = [
        Executable('main.py', base=base, target_name = 'temEatsss.exe')
    ]

    data_files = [
       ("templates/", "templates/"), ("static/", "static/"), "mydatabase.db"
    ]


    build_options = {'packages': ["os", "sys"],  'excludes': [], "include_msvcr": True, "includes": ["jinja2", "jinja2.ext", "pathlib", "flask", "pkg_resources"], "include_files": data_files}


    setup(name='teMeats',
          version = '1.0',
          description = '',
          options = {'build_exe': build_options},
          executables = executables)

https://github.com/samfisherirl/flask-jinja2-cx_freeze-setup.py/blob/main/README.md

© www.soinside.com 2019 - 2024. All rights reserved.