Before installing Flask, make sure you have Python installed on your system.
Open a terminal or command prompt and run:
python --version
or
python3 --version
Using a virtual environment helps manage dependencies and prevents conflicts between projects.
Navigate to your project folder and run:
mkdir flask_project
cd flask_project
python -m venv venv
For Windows:
venv\Scripts\activate
source venv/bin/activate
With the virtual environment activated, install Flask:
pip install flask
To verify the installation, run:
python -m flask --version
app.py
Inside the flask_project
folder, create a new file named app.py
and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask is running!"
if __name__ == "__main__":
app.run(debug=True)
Run the following command in the terminal:
python app.py
If everything is set up correctly, Flask will start the development server and display an output similar to: