Skip to content

Latest commit

 

History

History
 
 

Python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Getting Started with Python on Raspberry Pi

  1. Prerequisites
  2. Install Python 3
  3. Install Blinka or Install Grove
  4. Run your first program

Prerequisites

The following steps require a Raspberry Pi Zero W with Raspberry Pi OS Lite, a Linux operating system. To install it, see Raspberry Pi Zero W Setup. Make sure to configure Wi-Fi and enable SSH access so you can find your Pi, if your computer is in the same local Wi-Fi network.

Check Python Version

A fresh installation of the Raspberry Pi OS Lite has Python 3 preinstalled. Check this with:

$ python --version
Python 3.11.2

You need to install pip3 to be able to dd python packages to your project. Install pip3 as follows:

$ sudo apt-get install python3-pip
$ pip --version
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)

Optional: If you don't have Python3 installed, install it with:

$ sudo apt update
$ sudo apt install python3

The python documentation includes chapters on installing libraries and using GPIO in Python.

Raspberry Pi OS Bookworm

Note:
If you are using Raspberry Pi OS Bookworm, check this information.

Install Blinka

Note: The advantage of this approach is that you can use the same CircuitPython code on the Raspberry Pi as on the microcontroller.

Install the Blinka Python package with the following steps, based on this tutorial by Adafruit:

Frist create a Virtual Environment)

sudo apt install python3-venv
python3 -m venv env --system-site-packages

Activate the virtual environment (every time the Pi is rebooted):

source env/bin/activate

Install the Adafruit packages with:

cd ~
pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo -E env PATH=$PATH python3 raspi-blinka.py

and test it using the Blinka Test after a restart and activation of the virtual environment:

source env/bin/activate

Install Grove

Note: If you want to work with pure Python programming, you must install the corresponding Python package in order to access the hardware.

To access the GPIOs on the Pi and work with Grove sensors and actuators we use the Grove Python package.

Install the Grove Python package with:

$ sudo pip install grove.py

Here are some code examples by Seeed Studio.

Run your first program

Now, run your first program and install it permanently on your microcontroller.