Python is already installed on Raspberry Pi, but using it to create scripts and build various projects will often require some dependencies named libraries or modules. Even if you are good with Python, you might need some help knowing how to install new packages for Python on Raspberry Pi, which is exactly why I wrote this article.
Most of the Python packages for Raspberry Pi are available in the apt repositories. Searching for the library name with this tool will be the fastest way to install them. Another tool named “PIP” can also be used for some other modules not included in the default repositories.
Either way, I’ll explain everything in this article. I’ll start with APT because most modules can be installed with it, then I’ll introduce PIP and will conclude with a few tips on how to manage your Python libraries.
By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only them) to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun part. Get 10% off by downloading it today!
Install Python packages on Raspberry Pi
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
The easiest way to install new Python packages is to use APT (or the graphical version of it: “Add/Remove Software”). The most common Python libraries will be available in the repositories, often named something like “python3-library”.
Find Python packages on Raspberry Pi
A Python package is created to have reusable code at hand in any project. For example, a Python library can help with mathematics functions, interaction with an API, or a specific accessory. By using this kind of module, you don’t need to code the same functions in every new project.
It’s possible to create Python libraries yourself when you work on big projects, but most of the time, you’ll just download and install packages available in the Raspberry Pi OS repositories.
I’ll explain how to do this with the “Add / Remove Software” tool or the “apt” command line directly, but the first thing to know is the package name you need to install.
Recommended Python libraries on Raspberry Pi
The next section works well when you have a specific Python module that you are interested in (and which is available in the repositories), but maybe you are just looking for ideas and don’t know which packages you should install.
First, I recommend starting by reading this article that gives 15 cool Python project ideas on Raspberry Pi. It’s always better to start with the goal in mind and build something from there, rather than trying things without clear objectives.
Then, here are a few interesting Python libraries you can use with the method explained below:
- Sense HAT: It’s the most popular HAT for Raspberry Pi, and there is a great module available for it. You can read my tutorial here if you are interested.
- Gui Zero: The example I take in the next section, it’s an easy way to create a basic user interface in Python (forms, buttons, etc.).
- Minecraft Pi: Minecraft is preinstalled on Raspberry Pi. It’s a specific version that comes with a Python library to interact with the game, which is a great way to get started with Python, especially for kids. I also have a tutorial here.
- OpenCV: Looking for something more advanced? OpenCV is a computer vision library that you can use to experiment with machine learning.
Install new Python packages on Raspberry Pi
- On Raspberry Pi OS with Desktop, open the Add / Remove Software tool.
- Type the library name you are looking for in the search engine.
Let’s say “guizero” as an example.
- The tool will show you the corresponding packages:
- Check the corresponding box and click on “Apply” to install it on your system.
- A few seconds later, the package is installed and ready to use in Python.
Note: At the time of writing, Python 2 and 3 are installed on Raspberry Pi, make sure to install the packages for the version you are using in your script. Read this article if you are not certain.
If you don’t have a desktop environment or prefer to use the command line, here is the equivalent with commands:
- In a terminal, type the following command to find the exact package name:
apt search guizero
- Then install the corresponding package:
sudo apt install python3-guizero
Using PIP on Raspberry Pi
Even if the most common Python libraries are available in the Raspberry Pi OS repositories, there are many others that can’t be installed this way. Most developers use other methods to share their libraries with the world: PIP.
What is PIP on Raspberry Pi?
PIP is a package manager for Python packages, it’s preinstalled on Raspberry Pi OS and allow installing libraries that are not available in the default repositories.
It’s like a secondary repository, where it’s easier to upload and share a Python library, so most developers use it to maintain their code. For the end-user (you!), it’s also easy to use, as installing PIP packages is not more complicated than using apt.
You can access the web portal of this tool here, and use the search engine to find a Python library you can install on Raspberry Pi. I explain everything in the next sections.
Install PIP on Raspberry Pi
PIP is already installed on Raspberry Pi OS by default. If you are using another operating system or have removed it, here is how to install it:
sudo apt install python3-pip
Same thing as with any Python package, there are two packages available in the repository: python-pip and python3-pip. Make sure to install and use the version corresponding to your Python installation.
Install Python packages with PIP on Raspberry Pi
There is a search command available with PIP, but it seems that it isn’t available anymore (“PyPI’s XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future”), so the best way to find packages name is to use the website:
- Let’s say you are looking for a Python library to use the Twitter API.
- Go to pypi.org and look for Twitter in the search engine:
- Then browse the results to find the one corresponding to your needs. You can sort the results by relevance, trends, and last update.
Try to avoid libraries that are no longer updated, especially in this case.
- On the library page, you’ll generally get some documentation and the command to use to install the package.
pip install python-twitterOn Raspberry Pi, you need to use sudo, and also to use pip3 if you run your script with Python 3:
sudo pip3 install python-twitter
If there are any required dependencies, pip will install it automatically on your Raspberry Pi.
Where do PIP packages go once installed?
Once downloaded and installed, PIP packages are stored under /usr/lib/python3/dist-packages. The “show” command will display the exact location for each package: “pip3 show requests” for example.
And I will give you other commands you can use to get a better idea of what is installed on your Raspberry Pi in the next section.
Use Python packages on Raspberry Pi
Grab your free PDF file with all the commands you need to know on Raspberry Pi!
List of the installed Python libraries on Raspberry Pi
There are several methods to install Python packages and also to list those already installed on a Raspberry Pi. The more complete way is to use help(“modules”) in the console or a graphical equivalent with Thonny.
List Python modules from the console
- Open the Python console with:
- Type the help command to display all available modules:
It may take some time depending on the number of packages installed, and also display some errors and various debug information, but you should receive a complete list at the end.
Sale: 10% off today.
Get the eBook.
Do more with your Raspberry Pi, learn the useful concepts and take the shortcuts.
You miss half of the fun of using a Raspberry Pi if you don’t know anything about Python.
Thonny is the default Python text editor on Raspberry Pi OS with Desktop. If you are using this version, there is a hidden feature with the list of Python modules available:
- In the main menu, open Thonny Python IDE from the Programming submenu.
- Click on “Switch to regular mode” if you are in basic mode, and restart it.
- Then there is a menu on top, click on Tools > Manage packages.
- A window like this one will show up:
- You’ll receive the same information as with the previous method, in a more user-friendly way.
By the way, I didn't explain this method in the previous part, but it's also possible to install PIP packages directly in Thonny. Use the search engine on top and click on "Install" to add the new Python modules to the list.
List installed packages with PIP
With PIP, there is a command you can use to show all installed packages, here it is:
You’ll get a list with all of the installed packages and the corresponding version. It will be much quicker, but I’m not certain if it really includes everything (those not installed with PIP for example).
List installed Debian packages
And finally, the last way is to check the packages installed with apt, for example:
sudo dpkg -l | grep "python3-"
You can use this command on a fresh installation to see what is already included on your system.
Import a Python library in a script
Once the Python library is installed, with apt, PIP, or any other way, it can be imported into the source code by adding this line at the beginning of the script:
import <library>For example:
Then follow the corresponding documentation to use all the new functions offered by this module.
For example, my example is a library to create HTTP requests in Python, with a simple code like this:
r = requests.get('https://api.github.com/events')
Much simpler than doing it everything yourself from scratch 🙂
Python libraries are really powerful, so make sure to check if something that can help you already exists before spending hours creating your own functions.
Getting started with Python on Raspberry Pi
By the way, if you need help getting started with Python, you can read by tutorial for Python beginners here, and also check my new e-book, “Master Python programming on Raspberry Pi” in the store.
Python is an essential skill to learn when you want to start new projects on Raspberry Pi, the best ones often require Python code to automate the process. For example, most Raspberry Pi robot kits are programmed in Python.
Want to be a part of the RaspberryTips community? Join us here and get access to behind-the-scenes content, my thoughts and more while supporting me to continue posting on this website.
Raspberry Pi Resources
Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now
Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiples projects with step-by-step guides.
Download the e-book
If you just want to hang out with me and show your support, you can also join the Patreon community. I share behind-the-scenes content there and give you early access to my content. You’ll also get a shoutout when you join.
More details here
Need help to build something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now
You can also find all my recommendations for tools and hardware on this page.