Flask is a lightweight and flexible web framework for Python. In this article, we will explain step-by-step how to install Flask using pip, Python's package manager, with a real example.
Ensure Python is installed on your system. Open a terminal or command prompt and run:
python --version
If Python is not installed, download it from https://www.python.org/ and follow the installation instructions.
pip is Python's package manager and is often included with Python installations. To check if pip is installed, run:
pip --version
If pip is not installed, you can install it by following the instructions at pip's official site.
Create a new directory for your Flask project. For example:
mkdir flask_example cd flask_example
In your terminal, install Flask by running:
pip install flask
After installation, you can verify the Flask version by running:
flask --version
This ensures Flask has been installed successfully.
Create a new Python file named app.py
in your project directory. Add the following code:
from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello, Flask!" if __name__ == "__main__": app.run(debug=True)
This is a simple Flask application that responds with "Hello, Flask!" when accessed at the root URL.
To start the Flask application, run the following command in your terminal from the project directory:
python app.py
The application will start, and you can open your browser and visit http://127.0.0.1:5000/ to see the output.
You have successfully installed Flask using pip and created a simple Flask application. This forms the foundation for developing web applications with Flask.