Python Virtual Environments on Debian 12

Notes on setting up and managing Python virtual environments on Debian 12 (bookworm), including what to do with a venv copied from another machine.

System Python

Debian 12 ships Python 3.11 as the system Python. There is no python3.12 package in the bookworm repos (3.12 arrives with Debian 13).

python3 --version
# Python 3.11.x

Copied Venvs Don’t Work

If a directory tree containing a venv/ was copied from another computer, do not try to reuse it. Virtual environments hardcode absolute paths from the machine they were created on:

  • venv/bin/activate has the original path baked in
  • Scripts in venv/bin/ (pip, flask, etc.) have shebang lines pointing to the old location
  • venv/pyvenv.cfg points at the original Python interpreter, which may not exist here or may be a different version

Activation may appear to succeed, but pip and python will misbehave.

Fix: Recreate the Venv In Place

On the old machine (if still accessible), capture the installed packages first:

source venv/bin/activate
pip freeze > requirements.txt
deactivate

Copy requirements.txt into the project, then on wp7:

cd /path/to/project
rm -rf venv
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Creating a New Venv (Python 3.11)

For most projects, the system 3.11 is fine:

cd /path/to/project
python3 -m venv venv
source venv/bin/activate
python --version   # confirm

Install packages inside the venv as usual:

pip install flask

Activating and Deactivating

# Activate
source /path/to/project/venv/bin/activate
# Prompt shows (venv) prefix

# Deactivate
deactivate

If Python 3.12 Is Required

The version of Python used to create the venv is the version the venv gets. Since bookworm has no 3.12 package, the options are:

Installs 3.12 per-user without touching system Python.

# Build dependencies
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev libffi-dev liblzma-dev

# Install pyenv
curl https://pyenv.run | bash
# Follow the printed instructions to add pyenv to ~/.bashrc, then restart the shell

# Install and use 3.12
pyenv install 3.12
cd /path/to/project
pyenv local 3.12
python -m venv venv
source venv/bin/activate
python --version   # 3.12.x

Option 2 — Build from Source

Compile 3.12 and install with make altinstall so it lands as python3.12 alongside the system 3.11. Works, but more manual than pyenv.

Option 3 — Stick with 3.11

Unless a dependency specifically requires 3.12, Python 3.11 is well supported by all current packages and keeps the machine simple.

Quick Reference

Task Command
Create venv python3 -m venv venv
Create venv with specific Python python3.12 -m venv venv
Activate source venv/bin/activate
Deactivate deactivate
Freeze packages pip freeze > requirements.txt
Restore packages pip install -r requirements.txt

Debian 12 (bookworm) · system Python 3.11 · last updated July 2026