Install python on raspberry pi or debian
February 4, 2016 2 Comments
I have just started learning the python programming language. It is very different than C/C++ that I’m used to. So to Arduino and Java folks it will take some getting used to. But the reason that I am learning is the promise of cross-platform portability and powerful libraries/modules. If you could send an HTTP request in a few lines, or imagine that you write and test a program on PC and run it on a raspberry pi with no/minimal modification, would you stop complaining about weird syntax or loose data types and just learn the darn language? Well, I did.
So in order to use Python, you need to install it. Since I am interested in using raspberry pi with an arduino, I need the best serial port support that I can find. My conclusion is to use Python 3.5.1 and pyserial 3.0.1 (the latest of both as of the blog post).
On a windows PC, this would be the easiest. Download the installers and install the program.
On a linux PC or raspberry pi, python is included but is a lower version. The tutorial helps you install python on linux machines and raspberry pi’s.
This tutorial is based on this tutorial and pieces of other tutorials and information online:
To install the latest version of python, you need to build it from source code.
- Download the source code and unzip it in a folder.
- Update apt-get first
$ sudo apt-get update
- Install required tools to build Python from source. Debian/Raspbian Jessie will install libdb5.3-dev while Wheezy will install libdb5.1-dev. You need to install tk-dev to make Python’s IDE IDLE3.5 work
$ sudo apt-get install build-essential
$ sudo apt-get install tk-dev
$ sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
$ sudo apt-get install libdb5.1-dev libgdbm-dev libsqlite3-dev libssl-dev
$ sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
- Prepare for Pip (I didn’t do it but the tutorial I followed mentioned this, which seems a bit old)
$ mkdir -p ~/.pip/cache
$ echo ‘[global]’ > ~/.pip/pip.conf
$ echo ‘download_cache = ~/.pip/cache’ >> ~/.pip/pip.conf
- Make python in the unzipped source code folder (Python-3.5.1), install it in a folder that will not overwrite the current python version, such as in /usr/local/opt, delete upzipped source folder after done
$ cd Python-3.5.1
$ ./configure –prefix=/usr/local/opt/python-3.5.1
$ sudo make install
$ sudo mkdir /usr/local/opt/python-3.5.1
- Download pyserial source code and unzip in a folder.
- In the unzipped folder, run python 3.5 to install the module. If you used an alias to call python, then the install is in the default python version (3.4 on RPI) so don’t use an alias.
$ sudo /usr/local/opt/python-3.5.1/bin./python3.5 setup.py install
- Make aliases to run python 3.5 and idle more easily. You have to log out and back in after making them.
$ echo ‘alias python35=”/usr/local/opt/python3.5.1/bin/python3.5″‘ >> .bashrc
$ echo ‘alias idle35=”/usr/local/opt/python3.5.1/bin/idle3.5″‘ >> .bashrc
Now you are done! I forgot to mention that if you are installing Python on raspberry pi 1 or zero, the build process will take quite some time to complete. Log out and back in. In a terminal, type idle35 and you will be able to run your new Python.