Install Python 3.6. On Raspbian (English)

Do you want to use Python 3.6 on Raspberry Pi? When you run Python in Raspberry Pi, the version called is usually v2.7. This blog post will discuss how to change the default Python 2.7 into Python 3.6.

Before I begin the tutorial, I want to explain one thing which is commonly seen in Linux tutorials, the ‘$’ and ‘#’ annotations. When writing a command in Linux Terminal’s common documents, both annotations are commonly used as prefixes. The reason for that is in Linux Terminal, when the user isn’t accessing root, will show the ‘$’ annotation. If the Terminal is showing ‘#’, then the user is accessing root. In the document which showed Linux commands, ‘#’ means the command must be accessed as root (or with sudo)

The first thing that can be done is to check if your default Python is on version 3.x or 2.x. You can use this command to make sure of that

$ python --version
PythonVersionStart
Picture 1. Python version prior to upgrading to 3.6.4

There are a few things that must be done to change Python 2.7 into Python 3.6. The first thing is to ensure that several package dependencies must have been installed in Raspberry Pi. To make sure, you can use the command below

# apt-get update
# apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

After executing those programs, you can download the source code for Python 3.6 from Python’s official website. Python 3.6 source code is provided in tar.xz, which means you have to unpack it. But before you download it with wget, make sure your file is in the /tmp/ directory, so it will be deleted after you reboot

$ cd /tmp/
$ wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
$ tar xf Python-3.6.4.tar.xz
DownloadPythonSource
Picture 2. Result after downloading and extracting the source code from Python 3.6.4

After you finished extracting as in Picture 2, you can see that the source code is at /tmp/Python-3.6.4. To continue with the installation, access Python-3.6.4 directory

$ cd Python-3.6.4

After opening the directory, follow the process below to install Python 3.6.4:

$ ./configure
$ make
# make altinstall

After installing Python 3.6, you’ll see that Python default is still the same as the previous Python (in my case, still 2.7.13). To change the default Python that is being used, you have to use update-alternatives. First, make sure that you know where to install Python 3.6. and that the installed Python 3.6 can be accessed. Use both these commands to know where Python 3.6 is

$ ls /usr/bin/python*
$ ls /usr/local/bin/python*

After knowing the position of executable Python 3.6, do update-alternatives to add Python 2.7.13 and Python 3.6.4 as Python alternatives. Picture 3 shows the output from command to find out where the executable files from Python2.7, Python3.5 dan Python3.6. Add all those executable Python-s as alternatives from Python

FindingPythonExecutable
Picture 3. Python executable locations from root OS Raspbian directory

You will then find out the positions, which are /usr/bin/ for Python2.7 and Python3.5 and /usr/local/bin/ for Python3.6 which is just installed. Include the alternative /usr/bin/python using these commands:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
# update-alternatives --install /usr/bin/python python /usr/local/bin/python3.6 3

After adding it as the order above, Python 3.6 will automatically become the default for Python. If you run call python –version, it will show the output like in Picture 4

UpdateAlternativesPython

Picture 4. Automatic version change for Python into Python 3.6.4

If you want to reconfigure your Python back into Python 2.7.13, you can run the command as follows:

# update-alternatives --list python
# update-alternatives --config python

After executing the commands above, you’ll be asked to input the number you want to use as your default Python. Type 1 if you want to use Python 2.7.13 because of one reason or another (for example, the library you’re using still uses Python 2.7). After selecting which version you want, you can check the default version using ‘python –version’ command dan the result will show like in Picture 5.

RevertPythonTo27From36.PNG
Picture 5. Changing back the default version of Python into Python 2.7.13

You can use the configuration commands below to select which version you want to use. You can do this if the newest Python version is available and you want to add it into Raspberry Pi. If you have any questions, don’t hesitate to ask them in this blog. Have fun experimenting!

Tinggalkan komentar

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s

%d blogger menyukai ini: