Python 如何运行烧瓶应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29882642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:07:57  来源:igfitidea点击:

How to run a flask application?

pythonflask

提问by KarateKid

I want to know the correct way to start a flask application. The docs show two different commands:

我想知道启动烧瓶应用程序的正确方法。文档显示了两个不同的命令:

$ flask -a sample run

and

$ python3.4 sample.py 

produce the same result and run the application correctly.

产生相同的结果并正确运行应用程序。

What is the difference between the two and which should be used to run a Flask application?

两者之间有什么区别,应该使用哪个来运行 Flask 应用程序?

采纳答案by davidism

The flaskexecutable is a simple command line runner for Flask apps. It was introduced in Flask 0.11. It replaces the Flask-Scriptextension for adding commands. The docsdescribe how to use and add commands to this.

flask可执行文件是应用烧瓶一个简单的命令行转轮。它是在 Flask 0.11 中引入的。它取代了用于添加命令的Flask-Script扩展。该文档描述了如何使用和添加命令到这一点。

The python sample.pycommand runs a Python file and sets __name__ == "__main__". If the main block calls app.run(), it will run the development server. You don't have the ability to change the arguments to runwhen calling it.

python sample.py命令运行 Python 文件并设置__name__ == "__main__". 如果主块调用app.run(),它将运行开发服务器。您无法run在调用它时更改参数。

Both these commands ultimately start the Werkzeug development server, which as the name implies starts a simple HTTP server that should only be used during development. You should prefer using the flask runcommand over the app.run()method.

这两个命令最终都会启动 Werkzeug开发服务器,顾名思义,它会启动一个只应在开发期间使用的简单 HTTP 服务器。您应该更喜欢使用flask run命令而不是app.run()方法。

回答by Caner

Latest documentationhas the following example assuming you want to run hello.py(using .pyfile extension is optional):

最新文档有以下示例,假设您要运行hello.py(使用.py文件扩展名是可选的):

Unix, Linux, macOS, etc.:

Unix、Linux、macOS 等:

$ export FLASK_APP=hello
$ flask run

Windows:

视窗:

> set FLASK_APP=hello
> flask run

回答by DrakePro

it will work in cmd only if you type

仅当您键入时,它才能在 cmd 中工作

> pipenv shell 

start subshell in virtual environment first then type

首先在虚拟环境中启动子shell然后输入

> set FLASK_APP=hello
> flask run