Install Python 3.6 (or 3.7) and pip on a Raspberry Pi

Install Python 3.6 (or 3.7) and pip on a Raspberry Pi

Image for post

After years of faithful service Python 2.7 will not be maintained after January 1st, 2020. The news convinced me that it was the time to finally migrate the scripts I had running on my RPi to Python 3.

Adapting my code was an easy task except for the following annoying error when trying to load a JSON TypeError: the JSON object must be str, not ‘bytes’. Everything was fine on my Mac as Python 3.6+ does this automatically so I wanted to have the same version on my Raspberry Pi (which was on Python 3.5).

Installing Python3.6 on Raspbian Stretch his a matter of few commands to copy/paste. But after the install I had the following error each time I wanted to install a library using pip pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

I took me a few hours but I finally managed to fix the issue. If you are encountering the same SSL issues with pip and Python 3.6+ follow the steps below. All of them. In the same order. Even if you already have Python 3.6 installed. Trust me, I had to (re)build/install it 3 times because I wanted to save a few minutes.

Note: This also works for Python 3.7. You just need to adjust the names accordingly.

Hope that will help you!

Install Python

Install the required build-tools:

apt install libffi-dev libbz2-dev liblzma-dev libsqlite3-dev libncurses5-dev libgdbm-dev zlib1g-dev libreadline-dev libssl-dev tk-dev build-essential libncursesw5-dev libc6-dev openssl git

Download, build and install Python 3.6.5 (the most recent release is available on the official website):

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xztar xf Python-3.6.5.tar.xzcd Python-3.6.5./configure –enable-optimizationsmake -j -l 4sudo make altinstall

These steps took me around 4 hours on a Raspberry Pi 2 B.

Once finished, open ~/.bashrc file using nano:

sudo nano ~/.bashrc

and add new alias on the top of the file to change your default python executable:

alias python3=’python3.6′

You can also usepython by changing the previous command to alias python=’python3.6′. Once done, exit nano and source your .bashrc file:

. ~/.bashrc

Confirm the changes by checking your default python3 version:

python3 -V

The previous command should give you python3.6.5.

Install pip

To download and install pip run the following:

curl -O https://bootstrap.pypa.io/get-pip.pysudo python3.6 get-pip.py

And now you can install packages for Python 3.6 with pip-3.6!

No Responses

Write a response