How to change the default python version in Raspberry Pi

31,194

Solution 1

For those of you who like working directly with the filesystem, here is what I did:

  • type: sudo rm /usr/bin/python
  • type: sudo ln -s /usr/bin/python3 /usr/bin/python
  • type: ls -l /usr/bin/python
    • lrwxrwxrwx 1 root root 16 Jan 18 11:04 /usr/bin/python -> /usr/bin/python3
  • type: python -V
    • Python 3.7.3
  • alternative way:
    • type: sudo update-alternatives --config python
    • In my case no alternatives where found...

While you are at it, change pip default to pip3, slightly different process...

  • type: sudo mv /usr/bin/pip /usr/bin/pip2 # rename
  • type: sudo ln -s /usr/bin/pip3 /usr/bin/pip
  • type: ls -l /usr/bin/pip
    • lrwxrwxrwx 1 root root 13 Jan 18 11:19 /usr/bin/pip -> /usr/bin/pip3
  • type: pip -V
    • pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
    • NB: pip2 gets confused by switchover to default python3, as it starts with: #!/usr/bin/python

Solution 2

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.

Share:
31,194
Shyam3089
Author by

Shyam3089

Updated on September 19, 2021

Comments

  • Shyam3089
    Shyam3089 over 2 years

    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?