how to create a venv with a different python version

Viewed 17081

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

5 Answers

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:

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

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

# 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.

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.

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.

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

You can use anaconda:

conda create -n mypython3.8 python=3.8

For merely the sake of enough porridge in your stew.

Related