How to Install and Use Python Packages on Raspberry Pi?
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.
If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!
Install Python packages on Raspberry Pi
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”.
Introducing 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.
Related: How to Easily Install Apps on Raspberry Pi OS? (5 methods)
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 a 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, I explain everything on how to use this fantastic extension.
- 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 was preinstalled on Raspberry Pi on previous Raspberry Pi OS version. It’s a limited but comes with a Python library to interact with the game, which is a great way to get started with Python, especially for kids. Read my tutorial here to get started with Minecraft and Python.
- OpenCV: Looking for something more advanced? OpenCV is a computer vision library that you can use to experiment with machine learning.
Check my tutorial to learn how to setup OpenCV on your Raspberry Pi.
If you want to try other great Python libraries for the Raspberry Pi, I keep a selection for you on this website (click on the link).
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
And by the way, if you are entirely new to Python, it’s probably not the first tutorial you should read on this website. I recommend starting here, where I explain the basics of Python. You don’t need any library to create your first script.
Install new Python packages on Raspberry Pi
Now, let me explain how to install these packages, with or without a desktop environment.
With a desktop environment
- On Raspberry Pi OS with Desktop, open the Add / Remove Software tool (under Preferences).
- 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: On older versions of Raspberry Pi OS, 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 to avoid making a mistake. And check this other tutorial to change the default Python version.
With command lines
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
Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
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 another method 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, you may find two packages available in the repository: python-pip and python3-pip. Make sure to install and use the version corresponding to your Python installation (link to my tutorial where I give more details about this issue).
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.
For example:pip install python-twitter
Warning: since the switch to Bookworm, it is no longer possible to install PIP libraries directly in this way. A virtual environment is required, or you’ll get an “externally-managed-environment” error. It’s a bit complicated, but I explain everything in this other article.
In any case, make sure you’ve created one and activated it if you want to test the commands referenced here.
If there are any required dependencies, pip will install it automatically on your Raspberry Pi.
Where are Python packages installed?
Once downloaded and installed, Python packages are stored under /usr/lib/python3/dist-packages (for global installation) or under your virtual environment folder.
Using “pip3 show” will display the exact location for each package.
For example:pip3 show requests
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
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.
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
Use Python packages 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:
python3
- Type the help command to display all available modules:
help("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.
By the way, if it is unclear to you why Python is used on Raspberry Pi, you should click on that link to know everything about it. It’s an interesting story that any Raspberry Pi enthusiast should know.
Thonny
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:pip3 list
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:import requests
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:import requests
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. I start from scratch and explain only the important steps, the goal is to practice and achieve the results you want faster.
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.
Whenever you’re ready, here are other ways I can help you:
The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
Master your Raspberry Pi in 30 days: 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 multiple projects with step-by-step guides.
The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.
You can also find all my recommendations for tools and hardware on this page.