All jobs

Fix "AttributeError: module 'platform' has no attribute 'linux_distribution'"

Yes. An agent replaces platform.linux_distribution(), removed in Python 3.8, and finds the places where a broad except turned the error into empty output.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

platform.linux_distribution() was removed in Python 3.8, and code that calls it raises the error in the title. Often you never see that error, and the agent's first job is to find out which case you are in. fportantier/habu shows the quiet one. Its habu/lib/host.py builds a dictionary of host facts, with "distribution": platform.linux_distribution() on line 12, inside a try whose handler is except AttributeError: return {}. The command that prints it, habu/cli/cmd_host.py, shows the dictionary as JSON when it has content and [X] Unable to gather information when it is empty. So on Python 3.8 and later the habu.host command prints that one line on every Linux machine, and no traceback names the cause.

The agent proves this before changing anything. It runs gather_details() on Python 3.7, where the call still exists and prints DeprecationWarning: dist() and linux_distribution() functions are deprecated in Python 3.5, and gets all nine keys. It runs the same function on 3.8 and on a current interpreter and gets {}. Then it calls platform.linux_distribution() directly to show the hidden exception. The handler was meant for platforms where a field is missing; it now swallows the whole result, including the kernel, architecture and hostname that were never in doubt.

There are two replacements, and they suit different projects. platform.freedesktop_os_release() is in the standard library from Python 3.10 and returns the fields of the os-release file as a dictionary, with keys such as NAME, VERSION_ID and VERSION_CODENAME. The distro package works on older interpreters and offers distro.name(), distro.version() and distro.codename(); it still has a linux_distribution() function, but calling it warns that it is deprecated and only a compatibility shim. habu's setup.py lists fifteen requirements and distro is not among them, and its classifiers name Python 3.8, where the standard-library function does not exist. The agent lays out that trade and picks one: a new requirement, or a higher minimum Python.

It then narrows the handler so the next removal cannot hide the same way. Only the distribution lookup is guarded, and a host with no os-release file gets an empty distribution while every other key is still reported. The repository's file tested_on_windows.rst lists host as needing improvements on Windows, so that path is exercised, not assumed. The 13 files under tests have nothing for this command, so the agent adds a test that asserts the result is non-empty and has a distribution key. Finally it compares old and new values on the same machine and reports the differences, because consumers of the JSON will see them.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This project calls platform.linux_distribution() or platform.dist(), which Python 3.8 removed. Find every call. For each one, determine what a user sees today on a current Python: a traceback with \"AttributeError: module 'platform' has no attribute 'linux_distribution'\", or something quieter because an except clause catches the error. Reproduce both the old behaviour (on Python 3.7, where the function still exists) and the current behaviour (on the newest Python the project supports) and save the output of each. Replace the call. Compare two options and say which you chose and why: platform.freedesktop_os_release() from the standard library, which needs Python 3.10 or later, or the distro package, which is a new requirement. Do not use distro.linux_distribution(), which is deprecated. If an except clause was hiding the error, narrow it so it guards only the distribution lookup and the rest of the data is still returned on a host with no os-release file. Keep the shape of the returned data the same where you can, and list every value that differs between the old function and the new one on the same machine. Add a test that fails if the function returns an empty result on Linux. Update the documented example output if the project has one. Open a pull request with the before and after output on each Python version, the test results, and anything else you noticed but did not change.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes one line of logic and the structure around it: the distribution lookup moves into its own small function with its own handler, gather_details() returns the other eight keys regardless, and either setup.py gains distro or the minimum Python moves to 3.10. A new test file joins the others. The description shows the command's output on 3.7, the single failure line on a current Python before the change, and the full JSON after it.

The reviewer's attention belongs on the values, not the mechanics. The old and new functions do not answer alike. On a Debian 12.15 machine the removed function reports debian, 12.15 and an empty code name on Python 3.7, while distro reports Debian GNU/Linux, 12 and bookworm. Other fields move with the interpreter too: platform.libc_ver() answers 2.2.5 on that 3.7 build and 2.36 on 3.12. The description lists these differences so whoever parses the output downstream can decide whether they matter. The docstring in cmd_host.py, which shows a Fedora 29 example from Python 3.7.3, is updated or flagged.

Run it on every Linear issue labelled bug

A quiet failure like this reaches you as a user report, not an alert: someone writes that a command prints one unhelpful line. Add an automation on the Project with a linear trigger on linear.issue.labeled and the label your team uses for small confirmed bugs. The issue is added after the saved prompt as event text; it fills in no placeholder, so write the prompt to say that the report follows it. The trigger needs a Linear connection for the automation's runner, made in the console under Settings → Connections.

The trigger fires only when the person who adds the label is in your Rush org, and changes made by an app integration never fire it. The automation's max runs per hour caps how much work a burst of labelling can start.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Showing that a removed function still worked somewhere needs an interpreter most machines no longer carry, and showing the quiet failure needs a current one beside it. The agent sets up both in its own sandbox and runs the same function under each, and the outputs are kept with the session. A reviewer can read the nine keys from one run and the empty dictionary from the other without installing anything.

The more useful part is the search for the handler. A removed function behind a broad except gives no stack trace to search for, so the usual route from error message to fix does not start. An agent told to establish what the user sees, and to run the code to find out, reaches the handler in the first few commands and reports it as the real defect.

Frequently asked questions

What replaces platform.linux_distribution()?

On Python 3.10 and later, platform.freedesktop_os_release() returns the contents of the os-release file as a dictionary. For older interpreters, the distro package provides distro.name(), distro.version(), distro.codename() and distro.id(). Neither returns the same strings the removed function did, so compare them on your own distributions.

Why does my program print nothing useful instead of raising AttributeError?

Something catches it. A handler for AttributeError or a bare except around the call turns a removed function into an empty or default result. Search for the call first, then read the nearest enclosing try; that handler is usually where the output went.

Should I use distro.linux_distribution() as a drop-in?

No. It exists, but it emits a deprecation warning that describes it as a compatibility shim. Use the separate name, version and codename functions and build the tuple yourself if callers need one.

What happens on macOS or Windows?

There is no os-release file, so the lookup has nothing to read. The agent guards that one lookup and returns the rest of the data. It does not wrap the whole function in a handler again, which is what hid the problem.

More jobs like this: Turn a Linear label into a PR

Keep reading