全局 app 对象的问题
了解单个模块级 app 为何会限制您
全局 app 对象的问题 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Tempting Shortcut
Most tutorials create app at the top of a module and use it everywhere. It feels simple, but that single global object quietly limits how your project can grow. 🌱
One App, Forever
A module-level app is built the moment the file is imported. You get exactly one instance, with one fixed configuration, baked in for the whole process.
from flask import Flask
app = Flask(__name__) # created on importHard to Test
Testing wants a fresh app per run, ideally with test settings. A global app is already configured, so tests inherit your dev config and bleed state between them.
No Per-Environment Config
Dev, test, and prod each need different settings. With one global app, swapping config means editing globals or relying on import-time tricks that are easy to get wrong.
Circular Import Traps
Other modules import app to register routes, and app imports them back. This two-way dependency creates circular imports that crash on startup. 😣
Extensions Bound Too Early
Calling things like db = SQLAlchemy(app) at import time locks the extension to one app, before you even know which config to use.
db = SQLAlchemy(app) # bound too soonImport Order Becomes Fragile
Because everything hangs off one global, the import order starts to matter. Move one line and a route silently fails to register.
Multiple Apps Are Impossible
Sometimes you want two app instances, like one for an admin panel. A single global app makes running more than one in the same process awkward at best.
Config Set Before You Know It
At import time you often do not yet know if this is a test or prod run. A global app forces config decisions before that answer exists.
The Pattern That Fixes It
The cure is the application factory: a function that builds and returns a fresh, fully configured app on demand instead of one frozen global.
A Tiny Preview
Instead of a global, you call a function. Each call hands back a clean app you can configure however the situation needs.
def create_app():
app = Flask(__name__)
return appQuick Check
What is the core problem with a single module-level app object?
Recap
A global app is one frozen instance: hard to test, tough to configure per environment, and prone to circular imports. The factory pattern is the fix you will build next. 🚀
常见问题解答
「全局 app 对象的问题」课时是免费的吗?
是的 — 「全局 app 对象的问题」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「全局 app 对象的问题」这节课中我会学到什么?
了解单个模块级 app 为何会限制您 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「全局 app 对象的问题」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 全局 app 对象的问题
- 编写 create_app 函数
- 使用 init_app 初始化扩展
- 在工厂中注册蓝图