brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a python venv with system site packages reuses what your OS already installed

Python
~/tips/tip-python-venv-system-site-packages

sudo apt install python3-gi

Create a venv that can still see system wide packages like those from apt.

Some Python packages are painful to install with pip on a small server or a Raspberry Pi, for example GUI bindings or anything that needs to compile. When the distribution already ships them, you can let the venv see them.

sudo apt install python3-gi
python3 -m venv --system-site-packages .venv
.venv/bin/python -c "import gi; print(gi.__file__)"

The venv still has its own site-packages folder for everything you install with pip, and those win. It just falls back to the system packages for anything it doesn’t have. In .venv/pyvenv.cfg you’ll see the line include-system-site-packages = true.

That combination is handy: the heavy or OS specific packages come from apt and get security updates with the rest of the system, while your project’s own dependencies stay in the venv and out of the system Python. Debian and Ubuntu block pip install outside a venv these days, so this is often the cleanest way to mix the two.

You do give up some isolation. A pip freeze in this venv lists the system packages too, and a system upgrade can change a library version under your project without you touching it. I’d only use it on machines where the venv and the OS are meant to belong together, not for projects I deploy elsewhere.

Further reading: venv

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link