While running python app.py works for simple one-file experiments, professional Flask development relies on the Flask Command Line Interface (CLI) and robust environment configuration.
flask CLI CommandFlask includes a built-in CLI powered by the click library. The primary command is flask run.
When you execute flask run, Flask searches for an application entry point in the current working directory following this resolution order:
FLASK_APP environment variable.wsgi.pyapp.pyIn modern web development, configuration must be separated from source code (as mandated by The Twelve-Factor App methodology). We configure Flask using environment variables:
| Variable | Values | Purpose |
|---|---|---|
FLASK_APP | app.py, src.app, myapp:create_app() | Points to the entry point module or factory function |
FLASK_DEBUG | 1 (true), 0 (false) | Enables/disables live code reloader and interactive debugger |
SECRET_KEY | Hex string / random secret | Cryptographic key used to sign session cookies and CSRF tokens |
DATABASE_URL | Connection string | Database URI for SQLite, PostgreSQL, or MySQL |
python-dotenvManually setting environment variables in every new terminal session is error-prone. Flask natively integrates with python-dotenv:
When python-dotenv is installed, the flask CLI automatically detects and loads environment variables from two files in your project root:
.env: Sensitive variables (passwords, API keys, secrets) — never committed to Git..flaskenv: Public, project-wide development settings — committed to Git..flaskenv:.env:Instead of scattering os.environ.get() calls throughout your code, create a centralized config.py file using Python classes:
Loading the configuration into your Flask app:
.env and .flaskenv files when executing flask run?.flaskenv and .env?FLASK_APP is not set, which file names will Flask automatically inspect by default?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Virtual Environments & First Flask Application | Dynamic URLs & Type Converters |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.