Python Virtual Environments

Alexis Chilinski
The Startup
Published in
2 min readJul 19, 2020

I have been primarily focusing on Python recently (if that wasn’t obvious already) and I’ve been having some trouble understanding why virtual environments exist. I’ve never used one for other languages, so why use one for Python?

Python has interesting ways of storing/managing libraries and packages. Packages can be stored in your system using easy_install or pip (see differences between the two here). Python projects that require these packages/dependencies can then pull them from wherever they were stored in your system. This is great for system packages, meaning packages that are universally used by Python, or part of the normal Python setup. However, this can get tricky for site-packages. The latter refers to any project-specific dependencies. So what if you have two projects that both require the same package but different versions of the package? Your system will only store one version of the package, but if you utilize a virtual environment (venv) and install those dependencies within the venv, each project can have their specific dependencies instead of pulling from the system’s data.

Python3 has a venv feature built in, and once you’re inside your designated project folder (before creating the rest of your project files), you can run this command:

python -m venv venv

where the second ‘venv’ is what you want to call your venv. So, if I wanted to name my venv ‘cool_venv,’ then the command would be:

python -m venv cool_venv

If you view what’s in your project folder, you’ll see a folder for your venv. Next, you will need to activate it by running:

. ./cool_venv/bin/activate

again, where ‘cool_venv’ is where you put the name of your venv. Now you can start building your project and installing packages.

The tool for creating and activating a venv in Python2 is virtualenv. This can be installed system-wide with pip install virtualenv since it will be universally used across all projects.

Go into your project’s root directory for your venv. Then you can create your venv with python -m venv venv where the second venv is the name you choose for it. Once it’s created, you will need to activate it in order to start programming within it. To activate your venv, run . ./venv/bin/activate, again where that venv is the name you gave your venv. Now you should see some sort of notation in the command line that shows you’re in the venv, for example:

(venv)project_directory ~

Also, no matter the Python version, if you’re using a text editor like VSCode, make sure it’s using the correct interpreter (found at the bottom of the VSCode window). It should also include the venv name like this:

Python 3.7.6 64-bit (‘venv’: venv)

where the venv in quotes is the name of your venv. This will also let you know that you’re working in your venv. If you have a requirements.txt file already with all of your dependencies and versions, you will need to run pip install -r requirements.txt in order to properly install those dependencies into the venv. Otherwise, you can just run pip install within your venv to properly install packages.

--

--