问题描述
以下方式允许我启动Flask服务器.
The following ways allows me to launch the Flask server.
选项1:
set FLASK_APP = app.py
flask run
选项2:
set FLASK_APP = app.py
python -m flask run
选项3:
python app.py
使用这两种方法有什么区别?
What is the difference between using either of this?
推荐答案
$ python app.py
这是最简单,标准的调用方式 Python解释器以运行任何Python脚本.它不是Flask特有的. app.py 可能带有或不带有if __name__ == "__main__"
块(请参见如果__name__ ==怎么办? ; __ main __" ;:可以吗?),但是如果您要为Flask执行 操作,则需要 必须具有调用app.run()
的__main__
方法>.从 Flask文档:
This is the simplest, standard way of calling the Python interpreter to run any Python script. It is not specific to Flask. The app.py may or may not have a if __name__ == "__main__"
block (see What does if __name__ == "__main__": do?), but if you are going to do this for Flask, it is required to have __main__
method that calls app.run()
. From the Flask docs:
示例:
if __name__ == '__main__':
app.run()
同一文档也说明了为什么即使有效,也不建议这样做:
The same docs also state why even though this works, it is not recommended:
如果您需要根据主机环境修改运行配置(例如端口),则这种方法也存在问题.例如,在特定计算机上运行时,需要使用端口5500而不是默认的5000.您当然可以使用os.environ
和app.run(host=5500)
进行此操作,但这将是凌乱"的.根据与环境无关的与代码无关的配置修改代码.
This way is also problematic if you need to modify run configurations (ex. port) depending on the host environment. For example, you need to use port 5500 instead of the default 5000 when running on a certain machine. You can of course do this with os.environ
and app.run(host=5500)
, but it's going to be "messy" modifying the code based on environment-related configs that are unrelated to the code.
输入 flask
命令行工具.
Enter the flask
command line tool.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
$ set FLASK_APP=app.py
$ flask run --port=5500
您现在可以将代码维护为独立于任何外部环境配置.除此之外,flask
CLI工具还具有许多其他选项,用于配置和调试,例如启用/禁用DEBUG模式,列出路由(flask routes
)以及从.env文件获取环境变量.
You can now maintain your code to be independent of any external environment configurations. Aside from that, the flask
CLI tool has a lot of other options for configuration and debugging, such as enabling/disabling DEBUG mode, listing routes (flask routes
), and getting env vars from .env files.
还请注意,您的应用程序不必显式调用app.run
,并且__name__
现在不再是__main__
.这对于您的应用只是较大软件包的一部分和/或需要从其他目录运行的情况很有用.请参阅Flask文档的较大的应用部分.
Notice also that your app does not have to explicitly call app.run
and __name__
now isn't going to be __main__
. This is helpful for cases where your app is just part of a bigger package and/or it needs to be run from some other directory. See the Larger Applications section of the Flask docs.
最后,
$ python -m flask run
这是另一种运行Python脚本的标准方式 .它也不特定于Flask.从文档中:
This is another standard way of running Python scripts. It is also not specific to Flask. From the docs:
这意味着flask
将从调用的python
模块搜索路径.当您的环境具有多个Python版本并且您要确保使用正确的Python版本并与Flask一起使用时,此功能特别有用.当您为多个项目安装了多个Flask安装时,它也很有用.它显式设置要使用哪个Python解释器来调用flask
CLI工具.
This means flask
will be searched from the invoked python
module search path. This is particularly useful when your environment has multiple versions of Python and you want to make sure you are using the correct Python version and env with Flask. It can also be useful when you have multiple Flask installations for multiple projects. It explicitly sets which Python interpreter to use to call the flask
CLI tool.
$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
$ python -m flask --version
Python 2.7.16
Flask 1.0.3
Werkzeug 0.14.1
这篇关于使用flask run vs python app.py vs python -m flask run有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!