Python Virtual Environments - What are they?

Nov 1, 2025

A Python virtual environment is an isolated space, where we can work with different versions of Python packages, than what we have on our host system. This allows us to have a small confined space where we only have the packages we actually need.

Creating a Python virtual environment.

How do we actually create a virtual environment then? We use the builtin venv packages, that comes with a Python installation.

The command is structured like so: [path to python3 command] -m venv [path to where we want the virtual environment to live]

This then creates the virtual environment inside the folder we specifed.

$ python3 -m venv testproject
$ ll
drwxr-xr-x@ - ramiraz 14 Mar 21:06 testproject

Activating our virtual environment

To activate our virtual environment on Mac or Linux, we use the command “source”, and then the path to the activate file inside our virtual environment folder. This gives us a prompt with the name of our virtual enviromnent.

$ source testproject/bin/activate
(testproject) $

Exiting our virtual environment

To exit the virtual environment again. we can use a command built in to the environment called “deactivate”.

(testproject) $ deactivate
$

Install Python packages with pip

Our host system already has alot of Python packages installed. We can check which we have installed with the command: “pip3 list”. At the moment our host has 87 python packages installed. We could certanly just work with those.

Number of Python packages installed on my host system

[ramiraz@thor vEnvs]$ pip3 list | wc -l
87

But what happens then, when we need a different version of one of the packages we have installed, for a project. We can then install the one we need, but there would be a good chance that our other projects, who where depending on the old version will stop working.

To stop this from happening, we can use a virtual enviromnent for each of our projects. We can then just install the packages our project requires, since a newly installed virtual environment only comes with pip installed.

Number of Python packages installed inside my newly created virtual environment

(testproject) [ramiraz@thor vEnvs]$ pip list | wc -l
1

Lets say, that we create a project that requires the two packages request, and pytz (and ofcource any packages they have as dependencies), we can then install them with pip with the command: “pip install [name of package]”

(testproject) $ pip install requests pytz
...
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 23.0 MB/s eta 0:00:00
Using cached pytz-2024.1-py2.py3-none-any.whl (505 kB)
Downloading certifi-2024.2.2-py3-none-any.whl (163 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.8/163.8 kB 22.4 MB/s eta 0:00:00
Downloading charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 141.9/141.9 kB 59.5 MB/s eta 0:00:00
Downloading idna-3.6-py3-none-any.whl (61 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.6/61.6 kB 22.8 MB/s eta 0:00:00
Downloading urllib3-2.2.1-py3-none-any.whl (121 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.1/121.1 kB 41.4 MB/s eta 0:00:00
Installing collected packages: pytz, urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2024.2.2 charset-normalizer-3.3.2 idna-3.6 pytz-2024.1 requests-2.31.0 urllib3-2.2.1

If we now use the “pip list” command, we can see they are installed, along with a few dependencies.

(testproject) $ pip list
Package            Version
------------------ --------
certifi            2024.2.2
charset-normalizer 3.3.2
idna               3.6
pip                23.2.1
pytz               2024.1
requests           2.31.0
urllib3            2.2.1

Creating and using requirements.txt If we want an easily reproducable way of installing the the packages required by our project, we can create a list with the command “pip freeze > requirements.txt”, which creates a text file called requirements.txt with a list of the specific versions of packages we installed for our project

(testproject) $ pip freeze > requirements.txt

(testproject) $ cat requirements.txt
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
pytz==2024.1
requests==2.31.0
urllib3==2.2.1

We can now use this requirements.txt file inside a new virtual enviromnent, or send it along with our python program, so people who wants to use it can be sure to have the required dependencies installed.

They can then pass that file along to pip install with the -r flag

(testproject) $ pip install -r requirements.txt
...
Successfully installed certifi-2024.2.2 charset-normalizer-3.3.2 idna-3.6 pytz-2024.1 requests-2.31.0 urllib3-2.2.1

Deleting a virtual environment

Deleting a virtual environment is as simple as deleting the folder from the host system.

$ rm -rf testproject/

Naming the virtual environment

The standard convention is to create a folder to house our project, and then create a virtual environment inside this called venv (using venv as the path for this.). Our Python Project files go inside the project folder, but not inside the virtual enviromnent folder. The venv folder should be able to be deleted and recreated without any problems. It should also be added to .gitignore if we use git as source control.