python venv flask
- Create a virtual environment:
python -m venv venv
Activate the virtual environment:
On Windows:
bash
venv\Scripts\activate
- On macOS and Linux:
bash
source venv/bin/activate
- Install Flask within the virtual environment:
pip install Flask
Create a Flask application file, e.g.,
app.py
.Open the
app.py
file and define a Flask application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Save the file.
Run the Flask application:
python app.py
Open a web browser and go to
http://127.0.0.1:5000/
to see the "Hello, World!" message.To stop the Flask application, press
Ctrl+C
in the terminal where it's running.Deactivate the virtual environment:
deactivate