Python Libraries Every Developer Needs to Know for Raspberry Pi

Introduction to Python on Raspberry Pi

Python is an easy-to-use programming language that works natively on the Raspberry Pi. For example, it lets you control both hardware and software with simple commands. In addition, Python’s clear syntax makes it beginner-friendly. As a result, you can write code to monitor sensors or run software with ease.

Using Thonny IDE

The easiest way to start coding in Python on the Raspberry Pi is with Thonny, a built-in Python3 development environment. First, open Thonny from the desktop or applications menu. Next, you’ll see the REPL (Read-Evaluate-Print-Loop), a prompt where you can enter commands. Because it’s interactive, you get instant output on the screen. Moreover, the Shell window in Thonny makes testing code simple.

Understanding Python Libraries

Python libraries are collections of useful functions that save you from writing code from scratch. For instance, there are over 137,000 Python libraries today. Therefore, they help with machine learning, data science, data visualization, and many other tasks. Because they simplify development, libraries are essential for Raspberry Pi projects.

Installing Python Libraries with apt

Some packages are already in the Raspberry Pi OS archives. To install them, use apt. For example:

sudo apt update
sudo apt install python-picamera

As a result, these modules can be easily updated with:

sudo apt update
sudo apt full-upgrade

Installing with pip

Not all packages are in the Raspberry Pi OS archives. Sometimes, those that are may be outdated. If needed, you can install packages from the Python Package Index (PyPI).

First, install pip:

sudo apt install python3-pip

Then, install packages with pip3. For example:

sudo pip3 install simplejson

What is piwheels?

See also  Setting Up Your 4-Coin Acceptor

Some Python packages require compiling, which can be slow on older Raspberry Pi models. Because of this, piwheels offers pre-compiled packages (wheels) ready to use. Moreover, Raspberry Pi OS is already set up to use piwheels with pip. To learn more, visit piwheels.org.

Pre-Installed Libraries in Raspberry Pi OS

You get many pre-installed libraries with the standard Raspberry Pi OS. For example, you can view them in Thonny by clicking Tools, then Manage Packages. Because these are built-in, they’re easy to use right away.

These libraries help you control GPIO pins and hardware:

  • GPIO and Hardware Control Libraries
    • RPi.GPIO
    • RPIO.GPIO
    • wiringPi
    • pigpio
    • gpiozero

Picamera allows you to connect and control a CSI camera using Python.

  • Camera Interface
    • Picamera

In addition, Raspberry Pi OS includes powerful libraries for data and AI:

  • Data Science and Machine Learning
    • Sci-kit-learn
    • NumPy
    • OpenCV
    • SciPy
    • TensorFlow
    • Pandas
    • Matplotlib

For example, you can easily make web requests and parse data:

  • Web and Networking
    • Requests
    • BeautifulSoup
    • Scrapy
    • SQLAlchemy

Moreover, you can build simple games or graphics apps:

  • Graphics and Games
    • Pygame
    • Pillow (image processing)

Finally, Raspberry Pi OS includes many useful built-in Python modules:

  • Built-In Python Modules
    • csv
    • random
    • collections
    • datetime
    • re (for regular expressions)
    • math