|
| 1 | +--- |
| 2 | +title: Changes to Python 3 and pip on Void |
| 3 | +layout: post |
| 4 | +--- |
| 5 | + |
| 6 | +Happy Pythonmas! It's October, which means it's Python 3 update season. This |
| 7 | +year, along with the usual large set of updates for Python packages, a safety |
| 8 | +feature for pip, the Python package manager, has been activated. To ensure that |
| 9 | +Python packages installed via XBPS and those installed via pip don't interfere |
| 10 | +with one another, the system-wide Python environment has been marked as |
| 11 | +"externally managed". |
| 12 | + |
| 13 | +If you try to use `pip3` or `pip3 --user` outside of a Python virtual environment, |
| 14 | +you may see this error that provides guidance on how to deploy a virtual |
| 15 | +environment suitable for use with pip: |
| 16 | + |
| 17 | +``` |
| 18 | +This system-wide Python installation is managed by the Void Linux package |
| 19 | +manager, XBPS. Installation of Python packages from other sources is not |
| 20 | +normally allowed. |
| 21 | +
|
| 22 | +To install a Python package not offered by Void Linux, consider using a virtual |
| 23 | +environment, e.g.: |
| 24 | +
|
| 25 | + python3 -m venv /path/to/venv |
| 26 | + /path/to/venv/pip install <package> |
| 27 | +
|
| 28 | +Appending the flag --system-site-packages to the first command will give the |
| 29 | +virtual environment access to any Python package installed via XBPS. |
| 30 | +
|
| 31 | +Invoking python, pip, and executables installed by pip in /path/to/venv/bin |
| 32 | +should automatically use the virtual environment. Alternatively, source its |
| 33 | +activation script to add the environment to the command search path for a shell: |
| 34 | +
|
| 35 | + . /path/to/venv/activate |
| 36 | +
|
| 37 | +After activation, running |
| 38 | +
|
| 39 | + deactivate |
| 40 | +
|
| 41 | +will remove the environment from the search path without destroying it. |
| 42 | +
|
| 43 | +The XBPS package python3-pipx provides pipx, a convenient tool to automatically |
| 44 | +manage virtual environments for individual Python applications. |
| 45 | +``` |
| 46 | + |
| 47 | +You can read more about this change on Python's website in [PEP 668](https://peps.python.org/pep-0668/). |
| 48 | + |
| 49 | +To simplify the use of Void-based containers, all [Void container images](https://voidlinux.org/download/#containers) |
| 50 | +tagged `20231003R1` or later will explicitly ignore the "externally managed" |
| 51 | +marker. Containers based on these images will still be able to use pip to |
| 52 | +install Python packages in the container-wide environment. |
| 53 | + |
| 54 | +### Here Be Dragons |
| 55 | + |
| 56 | +If you really want to be able to install packages with pip in the system- or |
| 57 | +user-wide Python environment, there are several options, but beware: this can |
| 58 | +cause hard-to-debug issues with Python applications, or issues when updating with |
| 59 | +XBPS. |
| 60 | + |
| 61 | +1. Use pip's `--break-system-packages` flag. This only applies to the current invocation. |
| 62 | +2. Set pip's configuration: `pip3 config set install.break-system-packages True`. |
| 63 | + This will apply to all future invocations. |
| 64 | +3. Add a `noextract=/usr/lib/python*/EXTERNALLY-MANAGED` rule to your |
| 65 | + [XBPS configuration](https://man.voidlinux.org/xbps.d.5) and re-install the |
| 66 | + `python3` package. This will apply to all future invocations. |
0 commit comments