Score:0

Change python version only in a single virtual environment

th flag

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
vanadium avatar
cn flag
You did design this procedure yourself? Adding a PPA then installing python obviously will update the system version of python, and break your system.
Solebay Sharp avatar
th flag
No I did not, and as such I wasn't aware of what `PPA` did. I thought the problem was the way I setup my venv. Can I just remove the `PPA`?
vanadium avatar
cn flag
Yes, using ppa-purge you can remove the PPA and undo the changes. I cannot help specifically with setting up different python environemnts, unfortunatelly.
us flag
Using the Deadsnakes PPA does **not** update the system version of python. Both versions exist simultaneously.
Score:0
th flag

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

us flag
No. Using the Deadsnakes PPA to install python3.9 will **not** replace the system version of python.
Solebay Sharp avatar
th flag
So they exist simultaneously, What dictates which version of python is used?
us flag
When you run `python3`, you get system's default python (which is 3.6 in Ubuntu 18.04). When you run `python3.9`, you get python3.9. And obviously, when you run `python3.6`, you again get python3.6. I have not tried it, but as you have written in your answer, `python3.9 -m venv test_env` should give the intended results.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.