<<<Rasberry Pi Guides>>>

Installing Python 3

Having run numerous experiments on my Rasberry Pi I am ready to install the final version of my weather software and put it into production, but before I start I needed to clean everything up and do a clean install of Rasbian.

There are excellent instructions for this on the Rasbian Web Site

Once the new system has booted for the first time there are a number of commands to issue to update Python and make Python3 the default version on the operating system.

First make sure everything is upto date

First, update your system's package list by entering the following command:

pi@raspberrypi:~ $ sudo apt update
All packages are up to date.
    

Next, upgrade all your installed packages to their latest versions with the following command:

pi@raspberrypi:~ $ sudo apt full-upgrade
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    

Note that full-upgrade is used in preference to a simple upgrade, as it also picks up any dependency changes that may have been made.

Generally speaking, doing this regularly will keep your installation up to date for the particular major Raspbian release you are using (e.g. Stretch). It will not update from one major release to another, for example, Stretch to Buster.

However, there are occasional changes made in the Foundation's Raspbian image that require manual intervention, for example a newly introduced package. These are not installed with an upgrade, as this command only updates the packages you already have installed.

Then check that both versions of Python are installed

pi@raspberrypi:~ $ python -V
Python 2.7.16
          
pi@raspberrypi:~ $ python3 -V
Python 3.7.3
    

Next double check the update of Python Developer and Python PIP

I know this is overkill, but I like to be sure

pi@raspberrypi:~ $ sudo apt install python3-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-dev is already the newest version (3.7.3-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
        
pi@raspberrypi:~ $ sudo apt install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (18.1-5+rpt1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    

Finally make Python3 the default

pi@raspberrypi:~ $ python -V
Python 2.7.16
      
pi@raspberrypi:~ $ update-alternatives --list python
update-alternatives: error: no alternatives for python
      
pi@raspberrypi:~ $ ls /usr/bin/python*

pi@raspberrypi:~ $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
pi@raspberrypi:~ $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
      
pi@raspberrypi:~ $ update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.7
      
pi@raspberrypi:~ $ update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
      
Selection  Path        Priority   Status
------------------------------------------------------------
* 0      /usr/bin/python3.7   2     auto mode
1      /usr/bin/python2.7   1     manual mode
2      /usr/bin/python3.7   2     manual mode
      
Press [enter] to keep the current choice[*], or type selection number:
      
pi@raspberrypi:~ $ python -V
Python 3.7.3