DEV Community

Alex Chen
Alex Chen

Posted on

Quick Tip: Compare Two Python Environments with 5 Lines (Find Why 'Works on My Machine')

Quick Tip

"Works on my machine" is almost always a version skew. Instead of eyeballing two pip freeze outputs, diff them programmatically:

import importlib.metadata as im
import json, subprocess, sys

local = {d.metadata["Name"].lower(): d.version for d in im.distributions()}
prod = json.loads(subprocess.check_output(
    ["ssh", "prod-box", "python", "-c",
     "import importlib.metadata as im, json; "
     "print(json.dumps({d.metadata['Name'].lower(): d.version for d in im.distributions()}))"]))

for pkg in sorted(set(local) | set(prod)):
    if local.get(pkg) != prod.get(pkg):
        print(f"{pkg}: local={local.get(pkg)} prod={prod.get(pkg)}")
Enter fullscreen mode Exit fullscreen mode

Five minutes after adding this to my toolkit it caught a real one: pydantic 2.11 locally vs 2.6 in the Docker image — a validator behaved differently and only failed under production payloads.

I generated the SSH one-liner part with MonkeyCode (free tier: https://ly.cyberserval.tech/iIETXiF) — it got the nested quoting right on the first try, which is the part I always mess up by hand.

What's the nastiest version-skew bug you've ever hunted down?

Top comments (0)