MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Flask CLI & Environment Configuration0%
Study Materials
FlaskProgress 0%
1.1What is Flask & WSGI Architecture1.2Virtual Environments & First Flask Application1.3Flask CLI & Environment Configuration
HomeTutorialsFlaskFlask CLI & Environment Configuration
Dynamic URLs & Type Converters

Flask CLI & Environment Configuration

Beginner12 min read•Updated: 2026-09-16•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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.

1flask 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:

1pip 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:

1FLASK_APP=app.py
2FLASK_DEBUG=1
3FLASK_RUN_PORT=5000
4FLASK_RUN_HOST=127.0.0.1

Example .env:

1SECRET_KEY=9f82d8c47b5e4310a0e365cb551239c8
2DATABASE_URL=sqlite:///instance/app.db
3API_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:

1import os
2from datetime import timedelta
3 
4class Config:
5 # Base configuration shared across all environments
6 SECRET_KEY = os.environ.get('SECRET_KEY', 'fallback-dev-secret-key-change-in-prod')
7 PERMANENT_SESSION_LIFETIME = timedelta(days=7)
8 SQLALCHEMY_TRACK_MODIFICATIONS = False
9 
10class DevelopmentConfig(Config):
11 DEBUG = True
12 SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:///dev.db')
13 
14class TestingConfig(Config):
15 TESTING = True
16 SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
17 WTF_CSRF_ENABLED = False
18 
19class ProductionConfig(Config):
20 DEBUG = False
21 SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
22 SESSION_COOKIE_SECURE = True
23 SESSION_COOKIE_HTTPONLY = True
24 
25# Configuration dictionary mapping
26config = {
27 'development': DevelopmentConfig,
28 'testing': TestingConfig,
29 'production': ProductionConfig,
30 'default': DevelopmentConfig
31}

Loading the configuration into your Flask app:

1from flask import Flask
2from config import config
3 
4app = Flask(__name__)
5# Load configuration based on environment variable
6env = os.environ.get('FLASK_ENV', 'development')
7app.config.from_object(config[env])

Interactive Quiz• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Virtual Environments & First Flask ApplicationDynamic URLs & Type Converters

Practice Quiz

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

Mentor-Led Practical Training

Master Flask with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext