Change python version only in a single virtual environment


Answer: 1

36 hours ago

Gnome terminal will only run when python3.6 is used. So I created an environment and installed python3.9 like so:

#!/usr/bin/env bash

sudo apt install python3-venv
python3 -m venv test_env
source test_env/bin/activate

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
apt list | grep python3.9
sudo apt-get install python3.9
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3
python3 -V

However this causes the whole system to switch to python3.9 and prevents terminal from working. How can I prevent this?

Now, running source ~/.bashrc allows the non-venv terminal to continue working. And python -V reports the correct version of 3.6. But if I close the window I can't reopen it. I have to use a terminal in my IDE to reselect the older version of python. Running:

sudo update-alternatives --install /usr/bin/python3 python3

Shows that actually, version 3.9 is selected. Even though python -V actuallt reports running python3.6:

# Python 3.6.9

#  Selection    Path                 Priority   Status
# ------------------------------------------------------------
#  0            /usr/bin/python3.9    2         auto mode
#  1            /usr/bin/python3.10   2         manual mode
#  2            /usr/bin/python3.6    1         manual mode
#* 3            /usr/bin/python3.9    2         manual mode
Added by: Gardner McGlynn

Answer: 2

6 hours ago

As outlined by Vanadium, using PPA updates the system version of python. So even from the venv I was making a global change.

The solution is quite obvious; Python3 doesn't run Python3, it runs whatever version of python3 is installed on the system, so in my case...

python3 -m venv test_env

Was setting up a python3.6 venv. Running...

python3.9 -m venv test_env

Appears to have correctly setup a python3.9 venv. To hammer home the point, in order to run python code in ATOM IDE using Hydrogen, in my 3.9 venv I had to run...

python3.9 -m ipykernel install --user --name=atom_venv_py

So it's just a case of specifying which version of python you want to use in every instance where it deviates from the sys version (IPython kernel is the Python execution backend for Jupyter which allows Hyrdogen to run).

Added by: Jamarcus Fisher

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9