python venv flask

  1. Create a virtual environment:
python -m venv venv
  1. Activate the virtual environment:

  2. On Windows:

bash venv\Scripts\activate

  • On macOS and Linux:

bash source venv/bin/activate

  1. Install Flask within the virtual environment:
pip install Flask
  1. Create a Flask application file, e.g., app.py.

  2. 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)
  1. Save the file.

  2. Run the Flask application:

python app.py
  1. Open a web browser and go to http://127.0.0.1:5000/ to see the "Hello, World!" message.

  2. To stop the Flask application, press Ctrl+C in the terminal where it's running.

  3. Deactivate the virtual environment:

deactivate