Virtual environment

When working with Python projects, best practice is to have a separate virtual environment for each of them.

Each virtual environment has its own Python binary. When you install some Python package into your virtual environment, it'll be installed only into that specific environment. This means that you can have different versions of a single Python package in different virtual environments in the same machine. Virtual environments are also useful if you need to use different Python versions in your projects.

Creating new virtual environment

You can create all your virtual environments into a single directory (for example, .virtualenvs directory inside your home folder). This makes them easier to find.

python3 -m venv /path/to/new/environment

or

path/to/your/python -m venv /path/to/new/environment

Activating the virtual environment

Windows: path_to_virtual_env\Scripts\activate.bat
Posix: source path_to_virtual_env/bin/activate

Installing packages

After activating the newly created virtual environment, you can install new packages by using pip. For example if you want to install pytest:

python -m pip install pytest

it'll be installed into path_to_virtual_env/lib//site-packages. Note that the path to site-packages maybe slightly different depending on the operating system you are using.

You can list the installed packages and their versions by running:

python -m pip freeze