Flask CLI & Environment Configuration0%
Dynamic URLs & Type Converters

Flask CLI & Environment Configuration

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Flask CLI & Environment Configuration

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.


1. The flask CLI Command

Flask includes a built-in CLI powered by the click library. The primary command is flask run.

Bash / Terminal
flask run

When you execute flask run, Flask searches for an application entry point in the current working directory following this resolution order:

  1. 1
    The file or module specified by the FLASK_APP environment variable.
  2. 2
    wsgi.py
  3. 3
    app.py

2. Configuring Environment Variables

In modern web development, configuration must be separated from source code (as mandated by The Twelve-Factor App methodology). We configure Flask using environment variables:

VariableValuesPurpose
FLASK_APPapp.py, src.app, myapp:create_app()Points to the entry point module or factory function
FLASK_DEBUG1 (true), 0 (false)Enables/disables live code reloader and interactive debugger
SECRET_KEYHex string / random secretCryptographic key used to sign session cookies and CSRF tokens
DATABASE_URLConnection stringDatabase URI for SQLite, PostgreSQL, or MySQL

3. Automated Configuration with python-dotenv

Manually setting environment variables in every new terminal session is error-prone. Flask natively integrates with python-dotenv:

Bash / Terminal
pip install python-dotenv

When python-dotenv is installed, the flask CLI automatically detects and loads environment variables from two files in your project root:

  1. 1
    .env: Sensitive variables (passwords, API keys, secrets) — never committed to Git.
  2. 2
    .flaskenv: Public, project-wide development settings — committed to Git.

Example .flaskenv:

INI
FLASK_APP=app.py
FLASK_DEBUG=1
FLASK_RUN_PORT=5000
FLASK_RUN_HOST=127.0.0.1

Example .env:

INI
SECRET_KEY=9f82d8c47b5e4310a0e365cb551239c8
DATABASE_URL=sqlite:///instance/app.db
API_KEY=live_sec_prod_992182

4. Structured Configuration Classes in Flask

Instead of scattering os.environ.get() calls throughout your code, create a centralized config.py file using Python classes:

Python
import os
from datetime import timedelta
 
class Config:
# Base configuration shared across all environments
SECRET_KEY = os.environ.get('SECRET_KEY', 'fallback-dev-secret-key-change-in-prod')
PERMANENT_SESSION_LIFETIME = timedelta(days=7)
SQLALCHEMY_TRACK_MODIFICATIONS = False
 
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:///dev.db')
 
class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
WTF_CSRF_ENABLED = False
 
class ProductionConfig(Config):
DEBUG = False
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
 
# Configuration dictionary mapping
config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}

Loading the configuration into your Flask app:

Python
from flask import Flask
from config import config
 
app = Flask(__name__)
# Load configuration based on environment variable
env = os.environ.get('FLASK_ENV', 'development')
app.config.from_object(config[env])

Interactive Quiz

Practice Quiz

Question 1
What library enables Flask to automatically load .env and .flaskenv files when executing flask run?
Question 2
What is the difference between .flaskenv and .env?
Question 3
What setting enables live reloading and the browser traceback debugger in Flask 3.x?
Question 4
How does Flask load configuration settings from a Python class?
Question 5
If FLASK_APP is not set, which file names will Flask automatically inspect by default?
Next Lesson

Dynamic URLs & Type Converters

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext