Virtual Environments & First Flask Application0%
Flask CLI & Environment Configuration

Virtual Environments & First Flask Application

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

Virtual Environments & First Flask Application

Before building web applications with Flask, establishing an isolated virtual environment is critical. Virtual environments guarantee that your project's dependencies never conflict with system-wide Python packages or other projects on the same machine.


1. Setting Up an Isolated Virtual Environment

Python 3 provides the built-in venv module. Follow these commands in your project terminal:

On Windows (PowerShell / Command Prompt):

Bash / Terminal
# Create project workspace directory
mkdir flask_mastery_project
cd flask_mastery_project
 
# Create a virtual environment named .venv
python -m venv .venv
 
# Activate the virtual environment
.venv\Scripts\activate

On macOS / Linux:

Bash / Terminal
# Create and activate
python3 -m venv .venv
source .venv/bin/activate

When activated, your terminal prompt will display (.venv).


2. Installing Flask 3.x

Install the latest version of Flask using pip:

Bash / Terminal
pip install flask

To verify the installation and view installed dependencies:

Bash / Terminal
pip list

You will notice that installing flask automatically installs its key dependencies:

  • Werkzeug: WSGI utilities and routing
  • Jinja2: Template engine
  • MarkupSafe: HTML string escaping
  • click: Command Line Interface framework
  • itsdangerous: Cryptographic signing for session cookies
  • blinker: Signal dispatching (in modern Flask 3.x)

3. Creating Your First Minimal Flask Application

Create a file named app.py in your project root:

Python
from flask import Flask
 
# Initialize the Flask WSGI application
app = Flask(__name__)
 
@app.route("/")
def home():
return "<h1>Hello, World! Welcome to MSK Flask Mastery.</h1>"
 
@app.route("/about")
def about():
return {
"status": "success",
"message": "MSK Institute Flask Microframework Tutorial",
"version": "3.0.0"
}
 
if __name__ == "__main__":
app.run(debug=True, port=5000)

4. Dissecting the Code Line by Line

  1. 1
    from flask import Flask: Imports the central application class.
  2. 2
    app = Flask(__name__):
  • An instance of the Flask class is created.
  • __name__ is a Python special variable representing the current module's name ("__main__" when run directly).
  • Flask uses __name__ to locate templates, static assets, and resource folders relative to this file.
  1. 1
    @app.route("/"):
  • A Python decorator that registers a URL path mapping. When an incoming HTTP GET request matches "/", Flask invokes the decorated home() function.
  1. 1
    return "<h1>...</h1>":
  • Returns an HTML string. Flask automatically converts string returns into an HTTP 200 OK response with Content-Type: text/html; charset=utf-8.
  1. 1
    return {"status": "success", ...}:
  • In modern Flask, returning a Python dictionary automatically serializes to a JSON response with Content-Type: application/json.
  1. 1
    app.run(debug=True, port=5000):
  • Starts Werkzeug's local development server on http://127.0.0.1:5000.

5. Running the Application

Execute app.py directly:

Bash / Terminal
python app.py

Console Output:

Output
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 482-192-301

Open http://127.0.0.1:5000 in your web browser to view your live web application!


Interactive Quiz

Practice Quiz

Question 1
Why is __name__ passed into app = Flask(__name__)?
Question 2
What happens in modern Flask when a view function returns a Python dictionary?
Question 3
Which command activates a Python virtual environment on Windows PowerShell?
Question 4
What is the primary purpose of the debug=True flag in app.run() during development?
Question 5
Why is the built-in app.run() development server explicitly not recommended for production deployments?
Next Lesson

Flask CLI & Environment Configuration

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