Python 与应用程序的烧瓶混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14486370/
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-18 11:36:52  来源:igfitidea点击:

Flask confusion with app

pythonflask

提问by kasperhj

I am starting a flaskproject, and in my code I have

我正在开始一个flask项目,在我的代码中我有

from flask import Flask, render_template, abort
app = Flask(__name__)

Now what exactly is app?

现在到底是app什么?

I am following this guideand I am particularly confused about the structure because he has chosen to have directory named app/and is his app/__init__.pyhe has

我下面这个指南,我尤其感到困惑的结构,因为他已经选择了具有命名的目录app/,是他的app/__init__.py,他有

from flask import Flask
app = Flask(__name__)
from app import views

and in his app/views.pyhe has

并在他的app/views.py

from app import app

What the hell is it with all these app's?!

这些都是什么app鬼?!

采纳答案by codegeek

I think the main confusion is in the line:

我认为主要的困惑在于:

from app import app

You have a python package (a folder with __init__.pyfile) named "app". From this folder, you are now importing the variable "app" that you defined below in __init__.pyfile:

您有一个__init__.py名为“app”的 python 包(一个包含文件的文件夹)。从此文件夹中,您现在正在导入您在__init__.py文件中定义的变量“app” :

app = Flask(__name__)

Rename the folder from app to say "myproject". Then you will call

将应用程序中的文件夹重命名为“myproject”。然后你会打电话

from myproject import app

Also, you will import views as

此外,您将导入视图为

from myproject import views

回答by ?s??o?

The author made his code needlessly confusing by choosing a package name that is the same as Flask's usual application object instance name. This is the one you'll be most interested in:

作者通过选择与 Flask 的常用应用程序对象实例名称相同的包名称,使他的代码不必要地混乱。这是您最感兴趣的一个:

app = Flask(__name__)

Here is the documentation on the Flask application object:

以下是 Flask 应用程序对象的文档:

http://flask.pocoo.org/docs/api/#application-object

http://flask.pocoo.org/docs/api/#application-object

To avoid confusion, I recommend using the official Flask documentationinstead of that guide.

为避免混淆,我建议使用官方 Flask 文档而不是该指南。

回答by bereal

That's a bit confusing indeed, due to the poor names choice.

由于名称选择不当,这确实有点令人困惑。

  • app = Flask(__name__): here appis a WSGIapplication, it implements the corresponding interface and also supports whatever Flask has to offer us on top of that.
  • from app import app: imports exactly that appobject from the package app.
  • from app import view: For what heck he's importing viewsthere, is a bit of a mystery, I suppose he wants to make sure that the view bindings are executed. (I'd rather do that in run.py). In any case, that's a kind of importing loop between two modules which is at least confusing as well.
  • app = Flask(__name__):这app是一个WSGI应用程序,它实现了相应的接口,并且还支持 Flask 在此基础上为我们提供的任何内容。
  • from app import appapp从包中准确导入该对象app
  • from app import view:对于他在views那里导入的内容,有点神秘,我想他想确保执行视图绑定。(我宁愿在 中这样做run.py)。无论如何,这是两个模块之间的一种导入循环,至少也令人困惑。