Venv or Virtualenv? Which Should I Use?

Venv and Virtualenv are two of the most commonly used virtual environments for the Python language. Both have been integrated into the Python core since version 3.3 so using them is pretty much a piece of cake. While there is a difference between the two, they actually are made to work together. Which one you’ll use all depends on what your needs are. Do you need to use a different version of python for that one project that you don’t use anywhere else or do you just want to keep dependencies in sync?

Why Use a Virtual Environment for Python?

So I’ve been asked this a few times in the past by folks who read in various guides to always use a virtual environment with python, especially if it’s something you’ve downloaded off from GitHub. Now that advice doesn’t have the same magnitude of outcomes as when you were told to always wear a rubber but it can still cause some major frustration and headaches if not followed. The reason to always use a virtual environment is to keep both the project and your working environment synced up in terms of version control. Many times you’ll find code you’d like to implement on your own system but it uses deprecated or out of date dependencies and either you have to go through the hassle of downgrading everything, update the code to work with what you have, or start over. There are a couple different virtual environments but honestly the two easiest to implement are Venv and Virtualenv.

Venv - The Built-in Python Virtual Environment

So venv is a virtual environment that’s been built-into Python since version 3.3. So unless you’re using a very outdated setup you should already have it installed. It allows you to create an isolated Python environment using the current version of python you have installed. Each environment uses it’s own Python binary and an independent set of Python packages. It’s pretty fast and easy to use and setup and allows you to keep projects of all different types in-sync and usable. Here’s how to use it:

Create your new virtual environment by going to the root directory of your project and issuing the following command (i’m assuming you’re using some form of linux):

python3 -m venv .myproject

This will create a virtual environment in a new folder named .myproject (rename to whatever you want).

Now it’s installed, we need to activate it for it to work. Do that by using:

source .myproject/bin/activate

Your prompt will usually have a new character/notification on it when you’re in a virtual environment (at least in the majority of terminal setups) but you can always test it by typing

which python

It should return with the current file-path with .myproject/bin/python at the end

Now you can install whatever packages you want and it won’t affect the rest of your system. Easiest way to do that is with pip. Here are a few examples:

python3 -m pip install requests

#for specific versions of packages:
python3 -m pip install 'requests=2.11.2'

#from a local archive
python3 -m pip install requests-2.24.1.tar.gz

#if you're in the directory of the source code for a package you need to install use:
python3 -m pip install .

#If there's a requirements.txt file you can install all of those with:
python3 -m pip install -r requirements.txt

To export a list of all installed packages for that current virtual environment you can use freeze and it will show you both the package and current version:

python3 -m pip freeze

#or to have it write it's own requirements.txt file:

python3 -m pip freeze > requirements.txt 

to deactivate it when you’re done all you have to type is:

deactivate

Virtualenv Python Virtual Environment

Virtualenv is similar to venv, also standard in python since version 3.3, but the main difference is it allows you to create a virtual environment using different versions of Python. It works by being installed as a subset of the actual venv command so once installed it’s usage is almost identical except in how you create your environment.

First install virtualenv with pip:

pip install virtualenv

Now let’s go to the root folder of our project and run:

python(version) -m venv my-virtual-environment-name-here

#so like this:

python3.11 -m venv myproject

Now just like with venv it will create a virtual environment in your folder under .myproject/bin/

Activate it (to start it) with:

source .myproject/bin/activate

Installing dependencies is the same as it was with venv so for a bunch of examples just scroll up to the previous section.

End it by using deactivate:

deactivate

Notes

Make sure that regardless of what virtual environment you end up using to always have your .venv (or in our example: .myproject/) folder added to your .gitignore file. Otherwise your repo will download all the python files with it.