Updated: Mar. 22 2026 | Created: Mar. 22 2026
Flask Application — Write and Run Your First App
In this tutorial, you will create and run your first Flask application.
By the end of this guide, you will understand how a basic Flask app works, how to define a URL route, and how to start the Flask development server.
In the previous tutorial, you learned how to create a project folder, set up a virtual environment, and install Flask. In this guide, the next step is writing a simple Flask application and running it on your local machine.
Complete Flask Application
Here is the complete source code of a minimal Flask app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Below is a step-by-step explanation of how this code works.
1. Import the Flask Class
First, import the Flask class from the flask module.
from flask import Flask
The Flask class is used to create a Flask application instance. The flask module also provides other tools you can use later, such as request, render_template, redirect, and url_for.
2. Create the Application Object
Next, create a Flask application object by instantiating the Flask class.
app = Flask(__name__)
In this line:
appis the application object.Flask(__name__)creates the Flask app instance.__name__helps Flask know where your application files are located.
You will use the app object to register routes and configure your application.
3. Define a URL Route
Now define a route for the home page.
@app.route('/')
This line uses the route() method as a decorator.
In this example:
@app.route('/')tells Flask to match the root URL path/.- When a user visits that URL, Flask runs the function directly below it.
4. Create a View Function
After defining the route, create the function that handles requests to that URL.
def home():
return 'Hello, World!'
This function is called a view function.
When a user visits /, Flask executes the home() function and sends the returned text, Hello, World!, back to the browser.
5. Run the Flask Application
There are two common ways to run a Flask app.
Option 1: Run It with Python
If you want to start the app using the Python command, add this block at the bottom of your script:
if __name__ == '__main__':
app.run(debug=True)
Then run the file from the terminal:
# Windows
(venv) C:\Users\user\myproject> python app.py
# macOS/Linux
(venv) user@Users:~/myproject$ python3 app.py
In this block:
__name__ == '__main__'ensures the app runs only when the file is executed directly.app.run()starts the Flask development server.debug=Trueenables debug mode, which is useful during development.
Option 2: Run It with the Flask Command
You can also run the app using the Flask CLI without adding app.run() at the bottom of the file.
# Windows
(venv) C:\Users\user\myproject> flask --app app run --debug
# macOS/Linux
(venv) user@Users:~/myproject$ flask --app app run --debug
In this command:
--app apptells Flask that your application is inapp.py.runstarts the development server.--debugenables debug mode.
6. Open the Flask App in Your Browser
If the Flask development server starts successfully, you should see output similar to this in the terminal:
* Running on http://127.0.0.1:5000
Open that address in your browser:
http://127.0.0.1:5000
If everything is working correctly, you should see:
Hello, World!
Some terminals allow you to open the link by holding Ctrl and left-clicking it.
File Structure
Your project may look like this:
myproject/
│
├── venv/
└── app.py
The app.py file contains your Flask application code.
Complete Source Code
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
FAQs
Why do we use Flask(__name__)?
Flask(__name__) creates the application object and helps Flask locate resources such as templates, static files, and other application-related files.
Do I need if __name__ == '__main__':?
You only need that block if you want to run the file directly with python app.py.
If you use this command instead:
flask --app app run --debug
that block is optional.
Why is debug mode useful?
Debug mode automatically reloads the server when you save changes and shows detailed error messages in the browser. It is helpful during development, but it should not be enabled in production.
ModuleNotFoundError: No module named 'flask'
This error usually happens for one of these reasons:
- Flask is not installed
- The virtual environment is not activated
- You installed Flask in a different Python interpreter
- There is a typo in your import statement
To fix it, activate your virtual environment and make sure Flask is installed:
pip install flask
Address already in use
By default, Flask runs on port 5000. If that port is already being used by another program, you can change it.
Example:
app.run(debug=True, port=8000)
Then open:
http://127.0.0.1:8000