Home Linux Set up Python Packages in Raspberry Pi OS Bookworm

Set up Python Packages in Raspberry Pi OS Bookworm

0
Set up Python Packages in Raspberry Pi OS Bookworm

[ad_1]

Key Takeaways

  • Raspberry Pi OS Bookworm requires the set up of Python packages in a digital surroundings to forestall conflicts with the system model of Python.
  • You need to use the apt bundle supervisor to seek for and set up Python packages, but when the bundle will not be obtainable otherwise you want a more moderen model, you may want to make use of the pip device inside a digital surroundings.
  • Any modifications or installations made throughout the digital surroundings will not have an effect on the system model of Python.


MUO VIDEO OF THE DAY

SCROLL TO CONTINUE WITH CONTENT

Having points putting in Python packages utilizing the “pip” device in Raspberry Pi OS Bookworm? There are some further steps it’s essential take, involving the creation of a Python digital surroundings. Right here’s how one can do it.


What Has Modified in Raspberry Pi OS Bookworm?

For the reason that Raspberry Pi 5 is incompatible with earlier variations of Raspberry Pi OS, you will have to put in the brand new “Bookworm” model on it. This is without doubt one of the issues to think about when switching to Raspberry Pi 5 from an earlier mannequin.

In earlier variations of the Debian-based Raspberry Pi OS working system (Buster and earlier), it was attainable to put in Python libraries instantly, system-wide, utilizing the pip bundle administration device. That is not the case within the new Raspberry Pi OS Bookworm, nevertheless.

Because the Raspberry Pi documentation explains, the issue was that utilizing a Python-specific device akin to pip may trigger conflicts with the apt OS bundle supervisor.

Subsequently, from Bookworm onwards, when utilizing pip, packages have to be put in right into a sandboxed Python digital surroundings, which ensures that they will’t intervene with the system model of Python.

In the event you attempt to use the command pip set up [package name] anyplace else within the system, you’ll obtain an error beginning with this textual content:

 error: externally-managed-environment

&instances; This surroundings is externally managed
╰─> To put in Python packages system-wide, attempt apt set up
  python3-xyz, the place xyz is the bundle you are attempting to
  set up.

Seek for Python Packages With Apt

The very first thing to examine is whether or not the Python bundle you want is obtainable to put in utilizing the system-wide apt bundle supervisor. You possibly can seek for packages within the official repository utilizing the apt search command. For example:

 apt search numpy 

Observe the bundle identify, on this case python3-numpy (for Python model 3), after which set up it with apt (prefixed with sudo for the required superuser privileges for set up):

 sudo apt set up python3-numpy 

If the Python bundle you want will not be obtainable utilizing the apt bundle supervisor, otherwise you require a more moderen model of it, you wlll want to make use of the Python-specific pip device to put in it—inside a Python digital surroundings.

Create a Python Digital Surroundings

To put in a Python bundle with the pip device in Raspberry Pi OS Bookworm, you’ll first have to create a digital Python surroundings utilizing venv. We referred to as ours “muo-project”, however you should utilize any identify you need:

 python -m venv muo-project 

It can take a short time to finish, relying on which Raspberry Pi mannequin you’re utilizing. You’ll then want to alter listing to the newly created surroundings folder, which comprises a full Python distribution, and activate it:

 cd muo
supply bin/activate

The Python digital surroundings is now prepared to make use of, and the system immediate will probably be prepended with its identify—on this case, muo-project. This reveals that you just’re not utilizing the system model of Python, however the one inside your digital surroundings. So any modifications you make to it, or modules you put in, received’t have an effect on the system Python.

Creating a Python Virtual Environment in the terminal

Observe that when you reboot the Raspberry Pi, you will have to reactivate the Python surroundings to make use of it once more.

If you wish to create a Python digital surroundings with a duplicate of all of the Python modules presently put in on the working system degree, you are able to do so by including the –system-site-packages flag within the command. E.g : python -m venv –system-site-packages muo-project.

Set up Python Packages With Pip

From throughout the lively Python digital surroundings, now you can set up any packages you want utilizing the pip command. For instance, to put in the Stressberry system stress-testing device:

 pip set up stressberry 

It can then set up the module, together with any dependencies it requires, inside your Python digital surroundings. Observe that the module will solely be obtainable therein and never system-wide.

Installing a Python package with Pip

No Extra Python Bundle Conflicts

Whereas the set up of Python packages in Raspberry Pi OS Bookworm utilizing the pip device requires further steps, the benefit is that they solely then stay throughout the digital surroundings and subsequently can’t intervene with, or break, the system.

[ad_2]