> For the complete documentation index, see [llms.txt](https://education.raspberrypiaustralia.online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://education.raspberrypiaustralia.online/python-and-raspberry-pi.md).

# Python and Raspberry Pi

This documentation provides guidance on installing and managing Python packages on Raspberry Pi OS, with a focus on using Python virtual environments.&#x20;

Python 3 is <mark style="color:yellow;">pre-installed on Raspberry Pi OS</mark> and is crucial for many system functions.

It's important not to interfere with the system Python installation when installing third-party libraries.

There are two ways to install Python libraries on Raspberry Pi OS:&#x20;

1. Using `apt` to install pre-configured system packages (preferred method).&#x20;
2. Using `pip` to install packages not distributed as part of Raspberry Pi OS.

From the Bookworm version onwards, packages installed via `pip` must be installed into a Python virtual environment using <mark style="color:yellow;">`venv`</mark>.&#x20;

This change was introduced by the Python community to avoid conflicts between OS package managers and Python-specific package management tools.

### <mark style="color:blue;">**To create a virtual environment for each Python project**</mark>

1. Create a project directory and navigate into it.&#x20;
2. Create a virtual environment using `python -m venv env`.&#x20;
3. Activate the virtual environment using `source env/bin/activate`.&#x20;
4. Install packages using `pip` within the activated virtual environment.&#x20;
5. Deactivate the virtual environment using `deactivate`.

#### <mark style="color:blue;">Alternatively, you can create a single virtual environment for your user account</mark>

1. Create a virtual environment in your home directory using `python -m venv ~/.env`.&#x20;
2. Activate the virtual environment using `source ~/.env/bin/activate`.
3. Install packages using `pip` within the activated virtual environment.
4. Deactivate the virtual environment using `deactivate`.
5. Using virtual environments ensures that third-party packages are installed separately from the system Python, avoiding potential conflicts and issues with the operating system.

The documentation emphasises the importance of using virtual environments when installing Python packages on Raspberry Pi OS, especially from the Bookworm version onwards, to maintain a clean and stable system Python installation.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://education.raspberrypiaustralia.online/python-and-raspberry-pi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
