Python Libraries Every Developer Needs to Know for Raspberry Pi

Python is an easy-to-use programming language native to the Raspberry Pi. It allows you to control and monitor both hardware and software with effortless syntax and functions.

The easiest introduction to Python is through Thonny, a Python3 development environment. Open Thonny from the Desktop or applications menu. Thonny gives you a REPL (Read-Evaluate-Print-Loop), which is a prompt you can enter Python commands into. Because it’s a REPL, you even get the output of commands printed to the screen without using print. In the Thonny application, this is called the Shell window.

Python Libraries are a set of useful functions that eliminate the need for writing codes from scratch. There are over 137,000 python libraries present today. Python libraries play a vital role in developing machine learning, data science, data visualization, image and data manipulation applications and more.

Installing Python libraries

apt

Some Python packages can be found in the Raspberry Pi OS archives, and can be installed using apt, for example:

sudo apt update

sudo apt install python-picamera

This is a preferable method of installing, as it means that the modules you install can be kept up to date easily with the usual sudo apt update and sudo apt full-upgrade commands.

pip

Not all Python packages are available in the Raspberry Pi OS archives, and those that are can sometimes be out of date. If you can’t find a suitable version in the Raspberry Pi OS archives, you can install packages from the Python Package Index (known as PyPI).

To do so, install pip:

sudo apt install python3-pip

See also  How to Get Started with Linux Using the Raspberry Pi

Then install Python packages (e.g. simplejson) with pip3:

sudo pip3 install simplejson

piwheels

The official Python Package Index (PyPI) hosts files uploaded by package maintainers. Some packages require compilation (compiling C/C++ or similar code) in order to install them, which can be a time-consuming task, particlarly on the single-core Raspberry Pi 1 or Pi Zero.

piwheels is a service providing pre-compiled packages (called Python wheels) ready for use on the Raspberry Pi. Raspberry Pi OS is pre-configured to use piwheels for pip. Read more about the piwheels project at www.piwheels.org.

There are pre-installed libraries that come with the standard Raspberry Pi OS. You can see them by opening Thonny, click Tools then Manage Packages.

  • RPi.GPIO
  • RPIO.GPIO
  • wiringPi
  • pigpio
  • gpiozero
  • Picamera
    • Picamera allows you to interface a CSI camera using Python.
  • Sci-kit-learn
  • NumPy
  • OpenCV
  • SciPy
  • Tensorflow
  • Pandas
  • Matplotlib
  • Requests
  • BeautifulSoup
  • Pillow
  • Scrapy
  • SQLAlchemy
  • Pygame
  • Built in modules
    • Csv
    • Random
    • Collections
    • Datetime
    • Re – regular expressions
    • Math