且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用flask run vs python app.py vs python -m flask run有什么区别?

更新时间:2022-06-08 22:41:02

$ python app.py

这是最简单,标准的调用方式 Python解释器以运行任何Python脚本.它不是Flask特有的. app.py 可能带有或不带有if __name__ == "__main__"块(请参见如果__name__ ==怎么办? ; __ main __&quot ;:可以吗?),但是如果您要为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:

启动应用程序的另一种方法是通过 Flask.run()方法.这将立即启动本地服务器 flask脚本完全一样.

The alternative way to start the application is through the Flask.run() method. This will immediately launch a local server exactly the same way the flask script does.

示例:

if __name__ == '__main__':
    app.run()

同一文档也说明了为什么即使有效,也不建议这样做:

The same docs also state why even though this works, it is not recommended:

这在普通情况下效果很好,但不适用于 这就是为什么从Flask 0.11开始的flask方法是 推荐的.这样做的原因是由于如何重新加载 机制的工作有一些怪异的副作用(例如执行 某些代码两次,有时会崩溃而无任何消息,或者在 语法或导入错误).

This works well for the common case but it does not work well for development which is why from Flask 0.11 onwards the flask method is recommended. The reason for this is that due to how the reload mechanism works there are some bizarre side-effects (like executing certain code twice, sometimes crashing without message or dying when a syntax or import error happens).

如果您需要根据主机环境修改运行配置(例如端口),则这种方法也存在问题.例如,在特定计算机上运行时,需要使用端口5500而不是默认的5000.您当然可以使用os.environapp.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:

当用-m module-name调用时,给定的模块位于 Python模块路径,并作为脚本执行.

When called with -m module-name, the given module is located on the Python module path and executed as a script.

这意味着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