100

I have different venvs in my machine in which I have Python 3.10.

Now for a specific project, I realised that Python 3.10 is not suitable as some libraries are still not compatible. Therefore, when creating a new venv for a new project, I would like to downgrade Python, say to 3.8, only for this specific venv.

How can I do that? What should I type onto the terminal to do this?

PS: I use Visual Studio Code and its terminal to create venv.

13 Answers 13

85

The recommended way by python.org

The recommended way of managing virtual environments since Python 3.5 is with the venv module within the Python Standard Library itself.

Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments

That is not the same as virtualenv, which is a third party package, outside the Python Standard Library.

Source: https://pypi.org/project/virtualenv/

Dangerous to downgrade (and to upgrade)

Depending on if your system itself uses Python, it could be dangerous for system stability to change the system Python version. Your system might need exactly that version of Python. That is true with Ubuntu.


Instructions for Ubuntu

Tested on Ubuntu 20.04

Install another version of Python

Safer than downgrading or upgrading is installing other versions of Python on the same system.

For example, to install Python 3.9:

## Add the deadsnakes repository
me@mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa

## Update package lists
me@mydevice:~$ sudo apt update

## Install Python 3.9
me@mydevice:~$ sudo apt install python3.9

Install the venv package and create a venv virtual environment

## Install the venv package for Python 3.9
me@mydevice:~$ sudo apt install python3.9-venv

## Make a folder for venv virtual environments
me@mydevice:~$ mkdir ~/.venvs

## Create a new venv virtual environment with Python 3.9 in it
me@mydevice:~$ python3.9 -m venv ~/.venvs/my-venv-name

## Activate the new venv
me@mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me@mydevice:~$

Check versions within the venv virtual environment

## Check the Python version inside the venv:
(my-venv-name) me@mydevice:~$ python -V
Python 3.9.9

## Check the Pip version inside the venv:
(my-venv-name) me@mydevice:~$ pip3 --version
pip 21.2.4 from /home/me/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)

Deactivate the venv virtual environment

(my-venv-name) me@mydevice:~$ deactivate
me@mydevice:~$

Check versions outside any virtual environments

## Check Python version:
me@mydevice:~$ python -V
Python 3.8.10

## Check the Pip version:
me@mydevice:~$ pip3 --version
pip 20.0.2 from /home/me/.venvs/my-venv-name/lib/python3.8/site-packages/pip (python 3.8)

Install more Python versions

To install more Python versions, just change the version number from 3.9 to which ever version you choose, that is available from the deadsnakes repository.

Sign up to request clarification or add additional context in comments.

3 Comments

This answer is only applicable to Ubuntu (question is not tagged as such), relies on a third-party ppa which only works with Ubuntu LTS, and despite what it claims is in no way "The recommended way by python.org". I don't know why it is so highly upvoted.
The answer also applies to Mint and WSL (Windows Subsystem for Linux), where Ubuntu can be installed. Yes, a third party ppa is used, that is a correct observation. The first link in the answer takes you to python.org where the following statement resides: "The use of venv is now recommended for creating virtual environments.". The answer is probably upvoted because lot's of people find it useful, due to clear step-by-step instructions and because lot's of people use Ubuntu and WSL. The OP did not state what system the question was about, nothing about third parties either. Why the downvote?
Different versions of Python are also available in the AUR for people using this package manager. See for example: aur.archlinux.org/packages/python311 On more general systems, even when you don't have a package manager, you can install Python locally from source. It takes a bit more effort. See the official cpython repo for instructions: github.com/python/cpython Then, you find where the new python version you just installed is located and proceed as indicated here.
56

Simple and recent

Supposed that you have a different version of Python installed in your system. To check use the following command to check:

py --list

Output:

 -3.10-64 *
 -3.7-64

And you want to create a new virtual environment for python 3.7 on a 'test_env' directory. Run the following command:

py -3.7 -m venv test_env

Then activate the test_env by running the following command in Windows PowerShell:

.\test_env\Scripts\Activate.ps1

Or Linux:

source test_env/bin/activate

Check:

python --version

Output:

Python 3.7.0

8 Comments

The "py" command here refers to the Python launcher for Windows
This should be the accepted and most upvoted answer. This is extremely clean, easy and unhackish.
I agree that this here is the best answer
This can't be the best answer as this command does not exist on my system.
@nam, using Visual Studio Code - VS does not imply Windows. VS can be installed on Linux, Mac and Windows, as listed on their downloads page https://code.visualstudio.com/download
|
18

I believe the best way to work with different python versions in isolation is pyenv, managing virtual environments can be done with pyenv-virtualenv.

I think this article from Real Python does a good job at explaining how to manage different Python versions as well as different virtual environments.

For posterity, with the tools mentioned above you can do the following (once the proper Python versions are installed)

pyenv virtualenv <python_version> <environment_name>

# Then activate it

pyenv local <environment_name>

Now that you've created a virtual environment in the folder, it should be picked up any time you enter the folder. Visual Studio Code should also pick it up, as per its documentation.

P.S.: The reason I think it's a good approach is because it allows you to manage Python versions, as well as environments, with a single tool. Each version is installed only once, in one place, which should help, because it reduces complexity.

2 Comments

That refers to pyvenv, not pyenv which is a different tool @DannyK
14

You can have multiple Python interpreter versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the Python version you need and then specify its location when you create the virtual environment:

virtualenv -p <path-to-new-python-installation> <new-venv-name>

Example:

virtualenv -p  C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38

This will create a virtual environment called venv38 with Python 3.8.

5 Comments

many thanks. So I need any way to download python 3.8. I thought there was a command like downgrade or smtg in order to get it without manually going to download it myself.
Welcome. Yes, you need to download and install python 3.8 and then create the virtual environment based on it.
I have put myself in the folder I want to create, I downloaded the python version I want but when in VS cmd I digit your line above it says virtualenv is not recognised as a command..
You need to install it with: pip install virtualenv
actually this is not the recommended method anymore, check the official python docs, venv is the new standard.
7

You can use Anaconda:

conda create -n mypython3.8 python=3.8

For merely the sake of enough porridge in your stew.

1 Comment

This is great..for conda env, it wont create a 'venv'
4

For Windows users:

First, download Python 3.8 or any other Python version. Please do not add to the path if you have a preexisting installation of Python.

Then create a Python 3.8 venv by invoking the venv command:

Python38\python -m venv c:\path\to\myenv

If you are unsure of the path to the Python installation on your PC, you can run py -0p in a terminal session to verify. Your result will look like this:

 -V:3.12 *        C:\Users\Name\AppData\Local\Programs\Python\Python312\python.exe
 -V:3.8          C:\Users\Name\AppData\Local\Programs\Python\Python38\python.exe

Then activate your venv using:

c:\path\to\myenv\Scripts\activate.ps1

See Creating virtual environments.

Comments

3

You can do it by using the "virtualenv" library. It can be installed with command pip install virtualenv.

And then followed by the command virtualenv "name_of_your_environment" # No quotes

And then use the following code to activate your venv "name_of_your_environment"\Scripts\activate # NOTE: You MUST be at your directory where you created your env.

It’s for Visual Studio Code, but I prefer installing Conda and then creating env on the Conda prompt using Conda, which later you can access from Visual Studio Code too. It’s easy to activate that env from anywhere. Just type conda activate 'name_of_your_env' in the Visual Studio Code terminal.

Comments

3

The package manager uv, released in 2024, makes this very convenient.

It lets you use any Python version shown by the command uv python list, which it will install automatically, if required, separate from your system Python installation (in Linux under ~/.local/share/uv).

As of the current version, 0.8.6, the simplest command to just create an empty virtual environment which will use a specific Python version (for example, 3.8, which currently happens to also be the oldest supported version) is:

uv venv --python 3.8

Note that it will create an environment which does not contain some packages which are included when you use the standard library python -m venv, notably pip, since it assumes that you will use uv for package management.

If you just want to use uv for choosing the Python version, but not for package management, you can run the standard venv command, but with a specific Python version:

uv run --python 3.8 python -m venv

Or, if you plan to use the other Python version also outside the virtual environment, you can use uv python install 3.8 to create a symbolic link in your PATH (in Linux under ~/.local/bin), so that you can then use

python3.8 python -m venv

Comments

2

Without virtualenv, conda, third-party PPAs and without even installing Python anywhere in your system directories, completely contained in your local directory:

First, build the Python interpreter:

sudo apt install libffi-dev libbz2-dev liblzma-dev libncurses-dev

wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
tar -xvzf Python-3.9.2.tgz
cd Python-3.9.2
./configure
make
cd ..

Building Python doesn't take long.

You might need some header packages in Ubuntu to enable all Python features. The four above are what I needed for Python 3.9 to run a specific project correctly, found one-by-one by googling the error messages.

You can find other Python versions in the official Python FTP directory.

Then, use the build output to create your new venv:

./Python-3.9.2/python -m venv .
source ./bin/activate

Now if you just type python in your console, it should open a Python 3.9 console, until you run deactivate to exit the venv.

Comments

1

I had a similar case, and here is how I solved it with using pyenv to install different versions of the Python interpreter and venv to create a virtual environment.

Here is a workflow that you can give it a try:

Install pyenv

Here is a Arch Linux way of installing pyenv, and you can change the way you install it according to your operating system.

yay -S pyenv

Init pyenv for your shell

Do the things in the output after running the following command in your terminal. (In my case, edit the .zshrc file)

pyenv init

Install a new version of Python with pyenv install

Install the desired Python version with pyenv. I'll be installing version 3.10 you can change it according to your case.

pyenv install 3.10

Check version before the next step which is going to run shell command

python --version

Change your shell's Python version

Use pyenv shell to make your shell be set to the new version. This is not going to change your global settings of Python.

pyenv shell 3.10

Check the version (confirm it is changed when you compare with the previous check)

python --version

Create virtualenv (Python +3.6)

Go to the desired location (/path/to/your/project).

cd /path/to/your/project

Create the virtual environment in the location.

python -m venv .venv

Activate your virtual environment

source .venv/bin/activate

Confirm its version (it should be different than you global and be same with the version that you set with pyenv shell)

python --version

Close the current shell and open a new one. Then confirm global version of Python. By this way we'll make sure that our global system is not effected.

python --version

Go to the project directory

cd /path/to/your/project

Activate the venv

source .venv/bin/activate

Check the version (confirm it is different than the global version)

python --version

And you are all good!

Comments

1

In macOS, I've used Homebrew to install the Python interpreter, so I can invoke python with a chosen version to use and create the venv:

python3.8 -m venv venv

1 Comment

It depends on whether if you've got that version installed in the first place so marking this as acceptable answer.
0

An easy solution to this problem is to:

  1. First, install your desired version of Python from python.org

  2. During the installation wizard, check the 'add to path' option at the bottom of the wizard, so that you don't manually have to add Python to the path from the environment variables

  3. Go to the directory where you want to create the Python environment

  4. Run the command py -0 to check which Python versions you have available

  5. Then run the command py -//python version// -m venv //environment name// , for example py -3.9 -m venv env3.9, to create a virtual environment which uses your desired Python version

Comments

0

For venv you have to do it with

deadsnakes

For Ubuntu default repository all Python versions and latest ones are not there, so get the packages from repository and then install any version using apt:

sudo apt install python3.xx

And then while setting up the virtual environment use that Python:

python3.xx -m venv .venv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.