How to Install Python Packages in Raspberry Pi OS Bookworm

Having trouble installing the Python package using the "pip" tool in Raspberry Pi OS Bookworm? There are some additional steps you need to take, involved in creating the Python virtual environment. Here's how!

How to Search for Python Packages Using Apt

The first thing to check is whether the Python package you need is available for installation using the system-wide apt package manager. You can search for packages in the official repository using the apt search command. For example:

apt search numpy

Note the package name, in this case python3-numpy (for Python version 3), then install it using apt (with sudo prefix to get the superuser privileges needed for installation):

sudo apt install python3-numpy

If the Python package you need is not available using the apt package manager, or you require a newer version of that package, you will need to use the Python-specific pip tool to install the package - within the Python virtual environment.

How to create a Python virtual environment

To install a Python package using the pip tool in Raspberry Pi OS Bookworm, you first need to create a virtual Python environment using venv. The author calls his project "muo-project", but you can use any name you want:

python -m venv muo-project

It will take some time to complete, depending on the Raspberry Pi model you are using. You will then need to change directories to the newly created environment directory, containing the full Python distribution, and activate it:

cd muo source bin/activate

The Python virtual environment is now ready to use and a system prompt will be added before its name - in this case muo-project. This indicates that you are no longer using the system version of Python, but the version inside your virtual environment. So any changes you make to it or the modules you install will not affect the system Python.

 

How to Install Python Packages in Raspberry Pi OS Bookworm Picture 1

Note that if you restart your Raspberry Pi, you will need to re-enable the Python environment to use it again.

Note: If you want to create a Python virtual environment with a copy of all currently installed Python modules at the operating system level, you can do so by adding the flag --system-site-packages in Comeinand. For example:

python -m venv --system-site-packages muo-project

Install Python package using Pip

From within your active Python virtual environment, you can now install any packages you need using the pip command. For example, to install the Stressberry system stresstest tool:

pip install stressberry

It will then install the module, along with any dependencies it requires, in your Python virtual environment. Note that the module will only be available within and not system-wide.

How to Install Python Packages in Raspberry Pi OS Bookworm Picture 2

Although installing Python packages in Raspberry Pi OS Bookworm using the pip tool requires additional steps, the advantage is that they then only work in a virtual environment and therefore cannot interfere with or break the system .

4 ★ | 1 Vote

May be interested

  • How to Install Python on WindowsHow to Install Python on Windows
    if you want to start programming in python on your windows pc, you'll need to download and install a version of python. this wikihow teaches you how to install python (2 or 3) using the official windows installer, as well as how to fix the...
  • Learn Pi Imager, How to Use Raspberry Pi ImagerLearn Pi Imager, How to Use Raspberry Pi Imager
    the raspberry pi foundation recently released a new application for recording sd cards for the raspberry pi. the new program, called raspberry pi imager, launches march 5, 2020.
  • How to use Raspberry Pi to monitor Broadband speedHow to use Raspberry Pi to monitor Broadband speed
    in this article, we will show you how to use the raspberry pi to monitor the broadband speed. this project involves a bit of a fair setup, including downloading and installing several packages, writing python scripts, etc.
  • How to install an operating system for Raspberry PiHow to install an operating system for Raspberry Pi
    the article will show you how to install and run your new operating system on pi, as well as how to copy your perfect settings to fix errors quickly.
  • How to Install PythonHow to Install Python
    python is an interpreted, object-oriented, high-level programming language that is a great place for beginners to start learning how to program. python comes installed on macs and with linux, but you'll need to install it yourself if...
  • How to Install TkinterHow to Install Tkinter
    tkinter (tk) is a python default gui and comes with the python installation on linux, mac, and windows. since tk comes with most python installations, you don't generally need to install it yourself. since python 2 and python 3 vary so...
  • How to install NordVPN on Raspberry PiHow to install NordVPN on Raspberry Pi
    you've always wanted to get the most out of your raspberry pi and in today's article, we will share tips on how to easily configure openvpn encryption on your device.
  • How to install LibreELEC on Raspberry Pi 4How to install LibreELEC on Raspberry Pi 4
    the raspberry pi 4 is the most powerful piece of hardware to come from the pi platform. it has usb 3.0, more ram and faster cpu. for this reason, the pi 4 is perfect for a linux-based media player operating system like libreelec.
  • How to install Mathematica on Raspberry PiHow to install Mathematica on Raspberry Pi
    mathematica is an extremely powerful computational tool for processing data and math related things. in this tutorial, tipsmake.com will show you the easy steps to install mathematica onto your raspberry pi.
  • How to install Asterisk on Raspberry PiHow to install Asterisk on Raspberry Pi
    asterisk is a free, open source pbx that automatically manages your calls (perfect for a small business). in this tutorial, tipsmake.com will show you how to install asterisk on a raspberry pi.