4 min read

How to Compare Python Package Versions Between Environments with JSON After a Supply-Chain Scare

A practical way to check which package versions are installed in different Python environments: export them as JSON, sort and diff them, look for startup .pth files, and pin what you find.

UrwaTools Editorial Published
Contents

When a popular package is reported as compromised, the first question is simple: which of our environments has the bad version? Laptops, CI runners, containers and notebooks all drift apart over time, so memory is not a reliable answer. This guide shows a plain, repeatable way to export package versions as JSON, compare two environments with a diff, check for files that run at Python startup, and pin what you keep.

The example comes from a real news story. In November 2026, BleepingComputer reported that two versions of an agent framework package (1.26.1 and 1.26.2) were published to PyPI with credential-stealing code and quarantined within minutes. ProductListo's explainer on AI agent gateways, Google's MCP rule and the PyPI hijack covers what buyers of agent tools should ask. This article covers the hands-on part for developers.

In each environment you want to check, activate it and run:

python -m pip list --format=json > packages-laptop.json

Do the same on the CI runner or inside the container, and give each file a clear name, for example packages-ci.json. Each file is a list of objects with a name and a version. Keep a copy of the file with a date in the name so you can show later what was installed and when.

Raw output is often one long line. Paste it into a JSON formatter to read it, then use a JSON sorter so both files list packages in the same order. Sorting matters because two environments can contain identical packages in a different order, and a diff would otherwise show noise instead of real changes.

Open both sorted files in a JSON diff tool. You are looking for three things:

  • Version mismatches: the same package at different versions, which tells you where an upgrade slipped in.
  • Packages that exist in only one environment: often a tool someone installed by hand.
  • The affected version numbers: search the file for the package name from the advisory and compare its version with the reported bad versions.

If you need to share the result with a manager or an auditor, a JSON to CSV converter turns the list into a spreadsheet.

In the reported incident, the malicious code ran through .pth files, which Python processes when it starts, even if the package is never imported. The Python documentation for the site module explains how these files in the site-packages folder are handled. To find where that folder is, run:

python -c "import site; print(site.getsitepackages())"

List the .pth files in that folder and review any you do not recognize. Many legitimate packages ship one, so the goal is not to delete them blindly but to know what each one is for. A file whose name you cannot connect to a package you installed is worth a closer look.

The report advises these steps for anyone who installed a malicious version:

  1. Isolate the affected environment and stop running Python in it until it is secured.
  2. Rebuild from a known-clean version, in that case 1.26.0.
  3. Rotate every credential that was reachable from the environment, including cloud, GitHub and CI tokens.
  4. Check network logs for connections to the endpoint named in the advisory.

Rotating secrets is the step people skip, and it is the one that matters most, because stolen tokens keep working after you remove the package.

Once you know the good state, lock it. Export a requirements file with python -m pip freeze > requirements.txt and review changes in pull requests before upgrading. For stricter control, pip can verify file hashes with --require-hashes, which refuses any download that does not match the hash you recorded. Re-run the export in Step 1 on a schedule and diff it against the last known good file, so drift shows up as a small change instead of a surprise.

The same habit works for AI agents: keep a record of what happened, in a format a person can diff. UrwaTools has a guide on keeping a readable audit log of AI agent tool calls with JSON, and one on writing and checking a JSON schema for an AI agent tool. For broader habits, see the guide to a secure digital workspace.

If you are comparing agent frameworks, ProductListo lists LangChain, LlamaIndex, CrewAI and AutoGen, and its AI and ML buyer guide covers the wider field. Makers of ML and agent tools can submit a tool to the ProductListo directory, and sponsored placements are described on its write for us page.

The short version: export, sort, diff, check startup files, rotate secrets and pin versions. It takes under an hour for a small team and turns "are we affected?" into a question you can answer with a file.

Frequently asked questions

How do I list installed Python packages as JSON?
Run python -m pip list --format=json inside the environment. It prints a list of objects with each package name and version, which you can save to a file and compare.
Why sort the JSON before comparing two environments?
Two environments can hold the same packages in a different order. Sorting both files first means a diff shows real version differences instead of ordering noise.
What is a .pth file and why check it?
A .pth file in site-packages is processed when Python starts. Legitimate packages use them, but in a reported 2026 incident malicious code ran this way without the package being imported, so unknown .pth files deserve a review.
What should I do if I installed a malicious package version?
Isolate the environment, rebuild from a known-clean version and rotate every credential that was reachable from it. Removing the package alone does not make stolen tokens stop working.
How do I stop unexpected package upgrades?
Pin versions in a requirements file, review dependency changes in pull requests and consider pip's --require-hashes mode, which rejects downloads that do not match recorded hashes.
Written by UrwaTools Editorial

The UrwaTools Editorial Team delivers clear, practical, and trustworthy content designed to help users solve problems efficiently. Our articles focus on accurac...