In this tutorial you will learn about the Flask Installation and its application with practical example.
In this flask installation tutorial I will guide you through how to install Flask, a popular Python web framework:
Step 1: Prerequisites:
Before you start Flask installation, make sure you have Python installed on your system. Flask works seamlessly with Python 3.5 and newer versions.
Step 2: Virtual Environment (Optional but Recommended):
In order to maintain a clean and organized development environment, It’s a good practice to create a virtual environment for your Flask projects. This step isn’t mandatory but is highly recommended as it keep dependencies isolated. Let’s begin by running following commands in your terminal or command prompt:
- On Windows:
1 |
python -m venv myenv |
- On macOS and Linux:
1 |
python3 -m venv myenv |
Confirm that your terminal prompt displays your virtual environment name, indicating that it’s active. Replace “myenv” with your preferred virtual environment name.
Step 3: Activate the Virtual Environment:
Activate your virtual environment to isolate dependencies. Use these commands:
- On Windows:
1 |
myenv\Scripts\activate |
- On macOS and Linux:
1 |
source myenv/bin/activate |
Step 4: Install Flask:
With your virtual environment in place, proceed to install Flask and its prerequisites. Run the following command:
1 |
pip install Flask |
Step 5: Verify Flask Installation:
Now we check that Flask has been installed correctly in our system by running:
1 |
flask --version |
This command should display the installed Flask version.
Step 6: Create a Flask App:
You’re now ready to create your Flask web applications. Begin by setting up a new directory for your project and creating a Python file within it (e.g., app.py
) for your Flask application.
Step 7: Write a Simple Flask App:
In your app.py
file, create a basic Flask application. Here’s a minimal example:
1 2 3 4 5 6 7 |
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Flask!' |
Step 8: Run Your Flask App:
To start your Flask app, use this command from your terminal (ensure you’re in the project directory with app.py
):
1 |
flask run |
Now, access your app in web browser at http://localhost:5000
.
Conclusion:
Congratulations! You’ve successfully installed Flask in your system and created a basic Flask application.