Python Virtual Environment

Our video on Python Virtual Environment

I’ve covered this topic in great details, But it's a dynamic topic and it will evolve with future Python versions. So, I highly recommend checking the official documentation after watching this video, to learn more about the latest updates.

Here, I list some important updates:

Starting from version 3.13, venv now automatically creates a .gitignore file for Git.

The following topics weren’t included in the video as I felt they weren’t very important. However, I’ve decided to list them here in case you’re interested...

Upgrade a Virtual Environment

There are 3 things in a virtual environment that can be upgraded:

Virtual Environment Components
  1. Base Python version
  2. Core dependencies (pip, setuptools (this one was a core dependency before Python 3.12))
  3. Installed packages

Upgrading Base Python Version

Currently, I have Python version 3.11.9 and 3.12.5 installed in my system (check here).

py -0
Python Versions Installed

To upgrade an old virtual environment made with Python 3.11 to Python 3.12, run:

py -3.12 -m venv --upgrade .venv
py --version
Upgrading Base Python Version Upgrading Base Python Version

Note that this command only changes the Base Python version and doesn’t update anything else.

Upgrading Core Dependencies

To update its core dependencies, run:

py -3.12 -m venv --upgrade-dep .venv

Note that, -3.12 is attached to py mentions the Version of the Base Python of this virtual environment.

Before upgrading core dependencies, check the version of pip and setuptools (it was a core dependency till Python 3.11).

pip list
Packages Before Setuptools Upgrade

After upgrading core dependencies, pip has been upgraded to a newer version, but setuptools was at its latest version.

Packages After Setuptools Upgrade

Upgrading Installed Packages

Package versions before upgrade.

Packages Before Upgrade

To update a package installed here, use:

pip install --upgrade <package_name>

In this example, I upgrade pandas.

Upgrading Pandas

To update all the packages, first create a requirements.txt file,

pip freeze > requirements.txt

open it, replace == with >=, and run:

pip install --upgrade -r requirements.txt

Here, I display the process, I shorten requirements.txt into req.txt

Updating All Packages

The package versions after upgrade.

Packages After Upgrade

Before upgrading the Base Python version, always check if the packages required for your project are compatible with it. Similarly, check compatibility before upgrading any packages.

How to Have Different Versions of Python in the Same System (Windows) in Brief

Download multiple versions of Python from https://www.python.org/downloads/windows/. Remember that you cannot install two different versions that differ only by the last number, like 3.11.2 and 3.11.5.

While installing them, you must select all the options as shown below. This procedure is the same for all Python versions.

Installing Python

Now, open CMD or PowerShell and run py -0 to see all the python versions installed in your system.

py -0
Python Versions Installed in CMD

This star-marked one is the currently selected Python version, which you can see if you run py --version

Just keep in mind that in Windows, it is strictly recommended to use py to invoke Python instead of python.

By default, the py command selects the latest Python version. You can customize this by specifying the version explicitly, like py -3.11 or py -3.12.

py -3.11
py -3.12
Using Different Python Versions

Alternatively, you can create a system-wide environment variable named PY_PYTHON and set its value according to the desired Python version, like 3.11, 3.12, 3.13, etc. If you don't know how to create environment variables, visit: https://phoenixnap.com/kb/windows-set-environment-variable. I found their tutorial very good.