Uploaded: Nov. 29 2025 | Modified: Nov. 29 2025
How to Build Your First Flask Application (Step-by-Step Guide for Beginners)
If you're learning Python web development, building your first Flask app is the perfect starting point. This beginner-friendly guide will walk you through how to create, run, and understand your first Flask application from scratch.
By the end of this tutorial, you will have a working Flask web app running locally on your machine.
Note: If you haven’t installed Flask yet, check out our beginner installation guides for Windows, macOS, and Linux before continuing.
1. Flask Project Structure (Beginner-Friendly Overview)
Before writing code, your project folder should look like this:
myapp
│
├── .venv
│
└── app.py
myapp: your project’s main directory.venv: your virtual environmentapp.py: the main Python file for your Flask application
2. Create Your Flask App File (app.py)
Inside the myapp folder, create a Python file called:
app.py
All the code for your first Flask application will live here.
3. Import the Flask Class
Start by importing the Flask module:
from flask import Flask
This gives your code access to the Flask framework.
4. Initialize the Flask Application Object
Next, create a Flask application instance:
app = Flask(__name__)
This tells Flask where your application is located and initializes its core features.
5. Create Your First Route Using @app.route()
A route controls what your users see when they visit a specific URL.
Here’s how to define your home page (/):
@app.route('/')
6. Return a Response With a View Function
Now, write a function that returns a message to the browser:
def index():
return 'Hello, World!'
When someone visits your home page, this function runs.
7. Run the Flask Development Server
Add this code at the bottom of app.py:
if __name__ == '__main__':
app.run(debug=True)
debug=Truelets you see helpful error messages- Running the file will start your Flask development server
8. How to Run Your First Flask Application
Follow these steps:
Step 1 — Open your project directory
Windows
> cd \path\to\myapp
macOS/Linux
$ cd /path/to/myapp
Step 2 — Activate the virtual environment
Windows
myapp > .venv\Scripts\activate
macOS/Linux
myapp $ source .venv/bin/activate
Step 3 — Run the Flask development server
Windows
(venv) myapp > python app.py
macOS/Linux
(venv) myapp $ python app.py
Your terminal will display:
Running on http://127.0.0.1:5000
Step 4 — Open your Flask app in the browser
Go to:
You should see:
Hello, World!
Step 5 — Stop the server
Press:
CTRL + C
Step 6 — Deactivate your virtual environment
deactivate
Complete Working Example (Copy & Paste)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
FAQ (Frequently Asked Questions)
Why do I need app = Flask(__name__) in my Flask project?
This line initializes your Flask application and tells Flask where to find your resources, such as templates and static files. Without it, your app cannot run.
What does @app.route('/') mean in Flask?
@app.route('/') creates the home page URL for your application. When a user visits http://127.0.0.1:5000/, the route calls the function below it.
Why is my Flask app only showing a blank page?
Common causes include:
-
Your view function is missing a return statement
-
You spelled your route incorrectly
-
You forgot to restart the development server
-
Your virtual environment is not activated
What does debug=True do in Flask?
Enabling debug mode automatically reloads your app when you make changes and displays detailed error messages — extremely helpful for beginners.
How do I stop the Flask development server?
Press CTRL + C in the terminal where the server is running. This immediately stops the development server.
Do I need a virtual environment for my first Flask app?
Yes — using a virtual environment keeps your project’s dependencies isolated and prevents version conflicts with other Python projects on your system.
Why does my terminal say “ModuleNotFoundError: No module named 'flask'”?
This usually means:
-
Flask is not installed in your virtual environment
-
You activated the wrong virtual environment
-
You installed Flask globally instead of locally
-
Fix by running:
bash pip install flask
Where will my Flask app run after I start it?
By default, Flask apps run on:
http://127.0.0.1:5000
This is your local development server.
What should I learn after building my first Flask app?
- After this tutorial, the next recommended topics are:
- Flask Templates (
templates/folder) - Serving static files (CSS, images, JS)
- URL parameters
- Using Blueprints for larger apps
Flask is simpler and more flexible, making it ideal for beginners. Django is more structured and feature-rich for big projects.