deploys

Never move a Python virtualenv

Its scripts hard code the interpreter path, so gunicorn exits 203 and the API answers 502 for as long as it takes you to read the unit file.

Dawit Worku · 29 November 2026 · 2 min

The deploy built a release directory, created the virtualenv inside it, then moved the venv into place beside the application. Gunicorn failed with status=203/EXEC and the API returned 502 for about two minutes.

The shebang is absolute

Every executable in a virtualenv's bin/ carries a shebang pointing at the interpreter by absolute path. bin/gunicorn starts with the full path to the python that created it. pyvenv.cfg records absolute paths too.

Move the directory and none of that follows. The shebang points at a path that no longer exists, the kernel cannot exec it, and systemd reports 203. There is no warning at move time because mv has no idea it is breaking anything.

$ systemctl status app
* app.service - app gunicorn
     Active: failed (Result: exit-code)
    Process: 2114 ExecStart=/opt/app/backend/.venv/bin/gunicorn (code=exited, status=203/EXEC)
Two copies of a gunicorn script, one whose shebang resolves and one whose shebang points at a path that no longer exists, ending in systemd status 203 EXEC
The same executable before and after the move. The interpreter path is written once, when the virtualenv is created.

Build it where it will live

The fix is not a repair step, it is an ordering change. Build each release's venv at its final path and switch a current symlink when it is ready:

deploy.sh
rev=2026-11-29-a1b2c3
python -m venv /opt/app/releases/$rev/.venv
/opt/app/releases/$rev/.venv/bin/pip install -r requirements.txt
ln -sfn /opt/app/releases/$rev /opt/app/current
The venv is built where it will run, and the symlink is the only thing that moves.

The venv is never moved. The symlink moves instead, which is atomic, and rolling back is pointing it at the previous release.

If you have already moved one

Delete it and rebuild. --relocatable existed once, it was never reliable, and it is gone. Rebuilding from a lockfile takes less time than reasoning about which paths got rewritten.

※