how to create a venv with a different python version

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.

Leave a Comment

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş