Problemas con un objeto app global
Descubra por qué un único objeto app a nivel de módulo le limita.
Problemas con un objeto app global es una lección gratuita de Flask Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Flask Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Flask Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🚀
Preguntas frecuentes
¿La lección «Problemas con un objeto app global» es gratis?
Sí — el texto completo de «Problemas con un objeto app global» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Flask Academy, actualiza a CoddyKit PRO. El curso de Flask Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Problemas con un objeto app global»?
Descubra por qué un único objeto app a nivel de módulo le limita. Practicas Flask Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Flask Academy?
No se requiere experiencia previa. Flask Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Problemas con un objeto app global»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Flask Academy?
Sí. Cada lección de Flask Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Problemas con un objeto app global
- Escriba una función create_app
- Inicialice extensiones con init_app
- Registre Blueprints en la factoría