Python
Virtual Environment
Version Management
Python Downgrade
Programming Tips

Downgrade the Python version in a virtual environment (venv)

Codemia Knowledge Hub ·

Downgrading Python in a virtual environment starts with having the older Python version installed on your system, since each environment contains its own Python executable. You typically need this to match the requirements of certain libraries or projects.

How virtual environments relate to Python versions

A virtual environment is a self-contained directory tree with its own Python executable and copy of pip, used to manage project-specific dependencies. The most common tools are venv (in the standard library from Python 3.3) and virtualenv, a third-party library with extra features and compatibility across many Python versions.

Why you might downgrade

  • Compatibility: some libraries only support older Python versions.
  • Project constraints: legacy projects may need a Python environment that matches production.
  • Testing: you may need to test across several Python versions for backward compatibility.

Before you start

  • Python is installed on your system, preferably the version you want to downgrade to.
  • You have administrative privileges to install or uninstall software.

Downgrading Python in a virtual environment

Step 1: Check the current Python version

First, identify the Python version in your virtual environment. Activate the environment and run:

bash
python --version

Step 2: Save the installed packages

A virtual environment cannot switch its interpreter in place, so you will rebuild it. Record its packages first:

bash
pip freeze > requirements.txt
deactivate

Step 3: Recreate the environment with the older Python

Delete the old environment and create a new one with the older interpreter, for example Python 3.9:

bash
rm -rf venv
python3.9 -m venv venv

On Windows, use the launcher instead: py -3.9 -m venv venv. With virtualenv, pass the interpreter with virtualenv -p python3.9 venv.

Step 4: Reinstall packages and confirm the version

bash
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt
python --version

Problems after downgrading

  • Dependency compatibility: reinstalling dependencies can cause compatibility issues. Fix them by adjusting package versions in requirements.txt.
  • PATH conflicts: make sure your PATH is updated after installing a new Python version to avoid conflicts when creating or activating the environment.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms