Using Virtual Environments with Python

Working with Virtual Environments is very handy. It allows you to create a self-contained directory that contains a Python installation for a particular version of Python. Install all your dependencies in an environment that you can easily modify or remove.

Gettings started

Create a folder for a virtual environment inside your project.

python3 -m venv venv

The last venv could be anything, it will be the name of the folder that is created for the virtual environment. The name of the environment. Inside this, all dependencies will be installed.

Activate and deactivate

You need to activate your virtual environment to use it. Virtual environments are activated differently.

# Bash
source venv/bin/activate


# Powershell

With this activated, you’ll only use your virtual environment, meaning that you probably miss a few dependencies you need to install again. None of the dependencies you already have installed on your computer will be used in this environment.

If you need to deactivate your environment, you can do that with the deactivate command.


Python projects use the requirements.txt to handle all dependencies. You can create a file like that based on what you already have installed, or you can install everything to your environment that is written in the file.

# Save installed dependencies to file
pip freeze > requirements.txt

# Install from file
pip install -r requirements.txt