You are on page 1of 7

24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

Scientific Python for Raspberry Pi


A guide to setting up the Python scientific stack, well-
suited for geospatial analysis, on a Raspberry Pi 3.The
whole process takes just a fewminutes.

The Raspberry Pi 3 was announced two weeks ago


and presents a substantial step up in computational
power over its predecessors. It can serve as a
functional Wi-Fi connected Linux desktop computer
albeit underpowered. However its perfectly capable of running the Python
scientific computing stack including pandas, Jupyter, matplotlib, scipy, scikit-learn,
and numpy.

Despite (or because of?) its low power, its ideal for low-overhead and repetitive
tasks that researchers and engineers often face, including geocoding, web scraping,
scheduledAPI calls, or recurring statistical or spatial analyses (with small-ish data
sets). Its also a great way to set up a simple server or experiment with Linux.This
guide is aimed at newcomers to the world of Raspberry Pi and Linux, but who have
an interest in setting up a Python environment on these $35 credit card sized
computers. Well run through everything you need to do to get started (if your Pi is
already up andrunning, skip steps 1 and 2).

Step 1: Get thehardware

Assuming you have an availablephone charger, HDMI cable, mouse, and keyboard,
you can buyeverything else you need to get up and running for under $45. Heres
what youll need:

1. ARaspberry Pi($35)
2. A 5-volt 1-amp power supply (I just used an old Android charger with a micro-USB
cable, otherwise about $5)
3. A micro SD card with a full-size SD adapter (about $9)
4. An HDMI cable to connect your monitor (Ialready had one, otherwise about $5)
http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 1/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

5. USB mouse/keyboard (I already had


them, otherwise about $15 for a
basic set)
6. Optional: if youre running your
Raspberry Pi side-by-side with
adesktop computer, you can get a
cheap USB switchto switch your
mouse/keyboard from the computer
to the Raspberry Pi and back

GeoffBoeing
PhDcandidate,urbanplanningatUCBerkeley

Step 2: Install Raspbian OS on the Raspberry Pi

Now well install Raspbian (the Debian Linux OS tailored for Raspberry Pi) the easy
way, then boot the OS and connect to Wi-Fi.

1. Pop your micro SD card into its full-size SD adapter sleeve and stick it in your
computer
2. Download theNOOBSinstaller for Raspbian and un-zip it to your desktop
3. Download SDFormatter and install it (this tool is particularly useful if you
eventually want to refresh your Raspberry Pi system, as the Linux partitions
otherwise mightbe tricky to work with on your desktop computer)
4. Open SDFormatter, choose the SD card drive, click format
5. When the formatting is done, copy the NOOBS files from your desktop to the SD
card
6. Pop the SD card out of the adapter sleeve and into the Raspberry Pi
7. Connect your mouse, keyboard, HDMI, and power supply to the Raspberry Pi
8. Once NOOBS boots up, choose your language, select Raspbian, then click install

When the installation is finished, click ok and the new OS will boot up.The
Raspberry Pi 3 hasWi-Fi: in the top-right of the screen, click the Wi-Fi networks
panel item and choose your network to connect.

Step 3: Update packages

Next we update the existing software. Open a terminal window and run the
following commands, one at a time. The first line fetches updated package lists from
the repositories, and the second then fetches new versions of installedpackages. The

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 2/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

last two lines list the installed system packages and installed Python packages and
dump them to files, just for reference.

1 sudoaptgetupdate
2 sudoaptgetupgrade
3 dpkgl>~/Desktop/packages.list
4 pipfreeze>~/Desktop/pipfreezeinitial.list

apt-get is a Debian tool to install and update software packages. Well use it instead
of pip wherever we can because the packages come pre-compiled, meaning they take
seconds rather than minutes to install. When a Python package isnt available via
apt-get, well fall back on using pip to install (and compile) it.

Step 4: Install the Python basics

As we saw in theprevious steps file output, the Raspberry Pi comes with several
Python packages already installed. We need to supplement it with a few more
prerequisites. In the terminal window, run this command:

devpythondistlibpythonsetuptoolspythonpippythonwheellibzmqdevlibgdaldev

The build-essential package is required for building Debian packages;python-


dev,python-distlib, and python-setuptools provide several Python development and
packaging tools; python-pip and python-wheel are useful for installing Python
packages;libzmq-devis needed for Jupyter notebooks;libgdal-devis needed for
geospatial analysis with geopandas.

Step 5: Install pandas dependencies

Pandas has several recommended and optional dependencies that unlock


functionality or provide significant performance enhancements. To install them all,
run the following two commands:

1 sudoaptgetinstallxselxcliplibxml2devlibxsltdevpythonlxmlpythonh5py
2 sudopipinstallbottleneckrtree

The first commanduses apt-get to install theavailable recommended dependencies,


and the second commanduses pip to install the two that are not available in the apt
repositories.

Step 6: Install the scientific Python stack

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 3/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

Fortunately we can use apt-get to install all the massive, complex packages that make
up the Python scientific stack without having to compile everything. This makes the
process much, much faster.

1 sudoaptgetinstallpythonnumpypythonmatplotlibpythonmpltoolkits.basemap

If you need a specific version of these packages or want a more up-to-date version
than exists in the Debian repositories, you can use pip to install it, but be prepared
for a slow compilation process.

Step 7 (optional): Install other useful packages

Were all done! But if you optionally would like to install a few more useful packages,
run the following two commands:

1 sudoaptgetinstallpythonrequestspythonpilpythonscrapypythongeopypyth
2 sudopipinstalljupytergeopandas

The requestsPython package provides a graceful interface for making HTTP


requests, pil provides Python imaging capabilities,scrapyis a web scraping
framework,geopyprovides geocoding and geodesic distance functions,shapely
provides 2D geometry manipulation, and pyprojprovides cartographic
transformations. In the second command, jupyter provides interactive coding
notebooks and geopandasspatializes pandas.

Wrapping up

Our Python scientific stack is now all ready to use on theRaspberry Pi. Launch a
Jupyter notebook, load up some data with pandas, or plot a map with basemap. Due
to the Raspberry Pis memory constraints, you cannot load hugedata sets, bur
everything else works great. Its particularly good for repetitive, scheduled, or low-
overhead tasks such as geocoding and web scraping.

SHARETHIS:

8 1
More

March14,2016 gboeing Tech api,basemap,data,datascience,geocoding,geopandas,


geopy,geospatial,iot,ipython,jupyter,linux,matplotlib,numpy,pandas,pyproj,raspberrypi,raspbian,
science,scikit-learn,scipy,scrapy,shapely,statistics,statsmodels,webscraping

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 4/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

12 thoughts on Scientific Python for Raspberry Pi

Mike
March15,2016at01:10

Very useful, thank you. Would be something similar for python3? I guess that
changing python-package for python3-package would do the trick, but I am not
sure it is possible to make the full stack without problems.

In the other hand, it is useful to remember that we could use pandas chuncksize to
overcome a little bit the RAM limitation.

gboeing
March15,2016at09:16

Yes, great point about chunksize. In general, python3-package should give you
the python 3 version of most of these packages. Havent tested it yet but I will soon.
Interested to hear if anyones had succcess with python 3 on the Pi

Dima
September20,2016at06:36

I was unable to install scikit-learn for python 3 (3.4 to be exact). The process
failed with gcc internal error compiling scipy. More details are here:
http://stackoverflow.com/questions/26677655/scikit-learn-python3-on-raspberry-
pi

I would be very interested to hear from someone who did able to do it.

David
March15,2016at16:36

Nice!

It will be most interesting to see how this board works when full 64 bit support is
enabled. There is enough additional hardware that Im hoping things would run a bit
faster.

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 5/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

Al
March18,2016at13:17

I think Continuum Analytics support the raspberry pi hardware


(https://www.continuum.io/content/conda-support-raspberry-pi-2-and-power8-le).
Just go to https://www.continuum.io/downloads#_unix and give it a try

gboeing
March28,2016at16:10

Unfortunately, only a small handful of packages are available through


Anaconda/Miniconda on Raspberry Pi, and those built for the new ARM hardware
in the Pi 3 are even more limited as of now.

Pingback: Python # 7: Ubuntu 16.04 LTS to Ship Without Python 2 : IT


iAMX - -

Pingback: Week 11 | import digest

Mac
July18,2016at21:25

Thanks for laying this out so clearly.


I got an error running this:
sudo pip install bottleneck rtree

ImportError: cannot import name nanmedian

Any advice?

gboeing
July21,2016at15:46

Weird error. Try installing each package separately. That should at least isolate the
source of the error. Bottom line, you dont *need* bottleneck or rtree they just
enhance pandas speed and functionality.

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 6/7
24/10/2016 ScientificPythonforRaspberryPiGeoffBoeing

Valerio Giuseppe Cicchella


July29,2016at14:49

Hi thank you for the super clear guide! I am trying to launch a Jupiter notebook (by
giving the terminal the command Jupiter notebook, but it doesnt open anything it
just answer me command not found) can you help me? Should I install something
else to use it?

gboeing
July31,2016at12:16

The command is jupyter notebook not jupiter notebook

Proudly powered by WordPress

http://geoffboeing.com/2016/03/scientificpythonraspberrypi/ 7/7

You might also like