5

One day ago I did a fresh installation of Raspberry Pi OS Buster and after that I installed Python3.8 in my Raspberry pi following this tutorial. https://installvirtual.com/how-to-install-python-3-8-on-raspberry-pi-raspbian/

I added python alias to bashrc.

echo "alias python=/usr/local/bin/python3.8" >> ~/.bashrc
source ~/.bashrc

Now typing python in terminal showing Python 3.8.0 (default, Jun 8 2020, 13:17:16)

But when I run python3 it's showing Python Python 3.7.3

I added python3 alias pointing to python3.8 follwing above commands but still no luck. Programs from Geany still showing 3.7. I changed Geany's bulid commands to python(as I set default python to 3.8)

#!/usr/local/bin/python3.8

import sys
print("Python version")
print (sys.version)

Python version 3.7.3 (default, Dec 20 2019, 18:57:59)

I have two questions:

  1. How to run programs in Python3.8?

  2. Can I uninstall python3.7?

Shyam3089
  • 459
  • 1
  • 5
  • 20
  • 2
    What? The content doesn't match the title at all. – Sraw Jun 09 '20 at 05:38
  • The title does not match the question at all – planetmaker Jun 09 '20 at 05:40
  • Sorry I was editing a half edited previous question and accidentally changed title. I have edited title now. – Shyam3089 Jun 09 '20 at 05:41
  • @Shyam3089 : First verify what your alias is really set up to (by typing `alias python3`). It should display a message saying _alias python3=/usr/local/bin/python3.8_. Then verify the version by doing a `python3 --version`. Having said this, I must add that you have a someone unusual way to edit your `.bashrc`. Of course you can do it in that way, but there exist programs called _text editors_, and it is much easier to edit files with them, instead of using commands from the shell.... – user1934428 Jun 09 '20 at 05:59
  • Yes it's showing alias `python3='/usr/local/bin/python3.8'`. python3 --version showing `Python 3.8.0.` Actually I used `nano` to edit `.bashrc` – Shyam3089 Jun 09 '20 at 06:11

2 Answers2

18

For those of you who like working directly with the filesystem, here is what I did. Just type out the following commands one by one:

  1. sudo rm /usr/bin/python
  2. sudo ln -s /usr/bin/python3 /usr/bin/python
  3. ls -l /usr/bin/python
    This will produce something like this: lrwxrwxrwx 1 root root 16 Jan 18 11:04 /usr/bin/python -> /usr/bin/python3

Type python -V or python --version to have it show the current version you're running.

Alternate method (In my case no alternatives were found):
sudo update-alternatives --config python


While you're at it, change `pip` default to `pip3`. It's a slightly different process:
  1. sudo mv /usr/bin/pip /usr/bin/pip2 # rename
  2. sudo ln -s /usr/bin/pip3 /usr/bin/pip
  3. ls -l /usr/bin/pip
    This command will produce something like this: lrwxrwxrwx 1 root root 13 Jan 18 11:19 /usr/bin/pip -> /usr/bin/pip3 Type pip -V or pip --version to have it show the current version you're running.

Note: pip2 gets confused by switchover to default python3, as it starts with: #!/usr/bin/python

Community
  • 1
  • 1
John Brearley
  • 489
  • 1
  • 4
  • 10
  • 2
    IMHO, it should be pointed out that dealing manually with component configuration that is otherwise handled through package distribution mechanism is usually a bad idea for the sanity of the distribution tools and data. Proceed with care here. – Dfaure Nov 07 '21 at 10:35
  • Can you help please, are these instructions to make python 3.7 as default? – hyper-cookie Jun 29 '22 at 16:49
9

I have not seen an "official" solution from Raspberry Pi Foundation on changing which version of Python is active.

I won't go into why you probably shouldn't use an alias to run python, but will answer the more important titular question.

It seems we want to add our desired version of "alternatives" for the python binary.

On a fresh Raspberry Pi OS install, you just need one command:

# Adds `python3` as the alternate for `python` with priority `3`.
sudo update-alternatives --install $(which python) python $(readlink -f $(which python3)) 3

You can also add any number of other alternatives for python:

# Adds `python3.8` as the alternate for `python` with priority `3`.
sudo update-alternatives --install $(which python) python $(readlink -f $(which python3.8)) 3

To be nice, you should probably also add python2, at a lower priority:

# Adds `python2` as the alternate for `python` with priority `2`.
sudo update-alternatives --install $(which python) python $(readlink -f $(which python2)) 2

By default, the above will select the highest priority alternative automatically. To manually select a system-wide version, use:

sudo update-alternatives --config python

You can select different priorities that suit your needs. However, I've noticed that many other "alternatives" (notably editor) generally use multiples of 10 for official suggested versions. Keeping your priority values low might mean the official implementation of this (if it happens) will be compatible. You could also pick larger numbers...

Notes

dpkg -S /usr/bin/python reports it belongs to python-minimal, but uninstalling that removes all python2 from the machine (with autoremove). python3-minimal doesn't fix anything either.

Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45