Why replacing /usr/bin/python3 broke apt on my Ubuntu system
The problem
I installed Python 3.12 for a project and pointed /usr/bin/python3 to it, instead of using a virtual environment. Everything worked fine at first. A few days later, add-apt-repository failed with:
ModuleNotFoundError: No module named 'apt_pkg'
Why it happened
/usr/bin/python3 is not just a developer shortcut. Ubuntu uses it to run several of its own tools, including add-apt-repository, unattended-upgrades, and apport. These are Python scripts, and some of them depend on compiled Python modules built specifically for the Python version Ubuntu ships with, which was 3.10, not 3.12.
Once /usr/bin/python3 pointed at 3.12, these tools were running on an interpreter their compiled modules were never built for. Python looked for a module built for 3.12 and found nothing, because no such module existed.
The culprit
Installing Python 3.12 alongside the system Python was never the problem. That part is completely normal, plenty of machines run several Python versions side by side without any issue.
The real culprit was /usr/bin/python3 itself, a single, shared path. Every tool on the system that calls python3 without asking for a specific version follows that same path, including Ubuntu's own package management scripts. Those scripts don't need "some" Python 3, they need the exact one their compiled modules were built against. There's no version check before they run, so the moment /usr/bin/python3 changed, every one of those tools was quietly pointed at an interpreter it was never built for, and it stayed that way until one of them happened to run.
The fix
Keep the system Python untouched, and give the project its own environment instead:
sudo update-alternatives --set python3 /usr/bin/python3.10
python3.12 -m venv .venv
source .venv/bin/activate
This restores apt's original interpreter and gives the project Python 3.12 inside its own virtual environment. The OS manages its own dependencies, the project manages its own, and neither one has to interfere with the other.