Installing Flask
Key Concepts
Installing Flask involves understanding the following key concepts:
- Virtual Environment
- Package Management with pip
- Installing Flask
Virtual Environment
A virtual environment is an isolated environment for Python projects. It allows you to manage dependencies separately for each project, preventing conflicts between different versions of libraries. To create a virtual environment, use the following command:
python -m venv myenv
Activate the virtual environment:
- On Windows:
myenv\Scripts\activate
- On macOS/Linux:
source myenv/bin/activate
Package Management with pip
pip is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not part of the Python standard library. With the virtual environment activated, you can use pip to install Flask.
Installing Flask
Once the virtual environment is activated, you can install Flask using pip. Run the following command:
pip install Flask
This command downloads and installs Flask and its dependencies. To verify the installation, you can create a simple Flask application:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run()
Save this code in a file named app.py
and run it using:
python app.py
You should see output indicating that the Flask app is running, and you can view it by navigating to http://127.0.0.1:5000/ in your web browser.