45

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 VS and its terminal to create venv

Dark2018
  • 585
  • 1
  • 5
  • 7

6 Answers6

43

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.

Install another version of Python

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

For example, in Ubuntu 20.04, 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.

Emil Carpenter
  • 1,163
  • 10
  • 19
30

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
 -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 on Windows PowerShell:

> .\test_env\Scripts\Activate.ps1

Or Linux:

$ source test_env/bin/activate

Check:

python --version
Python 3.7.0
csmaster
  • 579
  • 4
  • 14
10

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. VSCode should also pick it up, as per its documentation.

P.S: The reason I think it's a good approach it's 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.

Nathan Furnal
  • 2,236
  • 3
  • 12
  • 25
  • 2
    I believe pyenv is deprecated - https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-venv – DannyK Jul 15 '23 at 16:29
9

You can have multiple python 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.

Dan Constantinescu
  • 1,426
  • 1
  • 7
  • 11
  • 2
    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. – Dark2018 Dec 20 '21 at 14:09
  • Welcome. Yes, you need to download and install python 3.8 and then create the virtual environment based on it. – Dan Constantinescu Dec 20 '21 at 14:12
  • 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.. – Dark2018 Dec 20 '21 at 15:03
  • You need to install it with: pip install virtualenv – Dan Constantinescu Dec 20 '21 at 15:13
  • actually this is not the recommended method anymore, check the official python docs, venv is the new standard. – user3426711 Feb 15 '22 at 19:53
4

You can use anaconda:

conda create -n mypython3.8 python=3.8

For merely the sake of enough porridge in your stew.

Paal Pedersen
  • 1,070
  • 1
  • 10
  • 13
2

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

then followed by 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.

its for VS CODE but I prefer installing conda and then creating env on conda prompt using conda which later you can access to vs code to and its easy to activate that env from anywhere just type conda activate 'name_of_your_env' on vs terminal

Zahra
  • 2,231
  • 3
  • 21
  • 41