How to create Python virtual environment

Step-by-step guide to creating and using Python virtual environments with venv for isolated package management.

Creating a virtual environment

  1. From project directory run the below command to create virualenv:
python3 -m venv env
  1. Activate the virtualenv using:
source env/bin/activate
  1. Confirm if the virtual environment is active:
which python
  1. To leave virtual environment:
deactivate

Installing a Package

Example:

python3 -m pip install requests

For more info: Installing packages using pip and virtual environments