0Pricing
Flask Academy · Aula

Problemas com um objeto app global

Entenda por que um app no nível do módulo limita você.

Problemas com um objeto app global é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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 import

Hard 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 soon

Import 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 app

Quick 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. 🚀

Perguntas Frequentes

A aula “Problemas com um objeto app global” é grátis?

Sim — o texto completo de “Problemas com um objeto app global” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.

O que vou aprender em “Problemas com um objeto app global”?

Entenda por que um app no nível do módulo limita você. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Flask Academy?

Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Problemas com um objeto app global”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Flask Academy?

Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Problemas com um objeto app global
  2. Escreva uma função create_app
  3. Inicialize extensões com init_app
  4. Registre Blueprints na fábrica
← Voltar para Flask Academy