It’s always recommended to use a virtual environment while running Python programs/applications where you need different Python packages to be installed using pip. Different Python projects need different python libraries. It’s a best practice to run different projects under different virtual environments. Here are simple steps to create a virtual environment to run your Python applications:
Step 1: Installing a virtual environment using pip.
python3 -m pip install --user virtualenv
Note: Python 3.3 or newer comes with venv and no additional installation is required. You can skip this step.
Step 2: Creating a virtual environment.
a. Check your current directory before creating a virtual environment. Below command will create a directory named “env” under your current directory.
python3 -m venv env
b. Now you can find a new directory created named “env”
Step 3: Activate the virtual environment.
source env/bin/activate
Step 4: Verify. Check the location of the Python interpreter to confirm that you are in the virtual environment you just created.
which python