All jobs

Fix "ImportError: cannot import name 'SafeConfigParser' from 'configparser'" on Python 3.12

Yes. An agent replaces SafeConfigParser in setup.py and the package, proves the install works on Python 3.12, and opens a pull request that names the release needed.

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

What the agent does

SafeConfigParser was renamed to ConfigParser in Python 3.2 and kept as an alias until Python 3.12 removed it. The error is most disruptive when the import is in setup.py, because then the package cannot even be installed: the build runs that file, the import fails, and pip reports a build error that says nothing about the user's own code. sebdah/aws-ec2-assign-elastic-ip has the pattern in two places. setup.py uses the class to read the version number out of aws_ec2_assign_elastic_ip/settings.conf, and aws_ec2_assign_elastic_ip/command_line_options.py uses it to read the same file for --version. Both wrap the import in a try that prefers the Python 2 module name ConfigParser and falls back to from configparser import SafeConfigParser.

That fallback is why the failure is confusing to read. On Python 3 the first import always fails, which is expected, and the handler's own import is the one that raises on 3.12. The agent reproduces it in a fresh virtual environment per interpreter. On Python 3.12, pip install . stops while building, with a traceback that ends at line 7 of setup.py and the title's message, plus the interpreter's suggestion Did you mean: 'RawConfigParser'?. On Python 3.11 the install succeeds and importing the class prints a DeprecationWarning saying the alias will be removed in 3.12. The warning had been there for years, in a place nobody reads.

The suggestion in the traceback is a trap. RawConfigParser does not interpolate %(name)s references, so swapping it in changes how values are read. The agent uses ConfigParser, which on Python 3 is the same class the old name pointed to, and checks the config file to be sure: settings.conf holds one section with one version key and no interpolation. It then decides what to do with the Python 2 branch by reading what the project claims to support. Here the classifiers name no versions and the requirements include boto3 and ec2-metadata, so it drops the branch and says so, instead of keeping an import path that can no longer be exercised.

With both files changed, pip install . completes on Python 3.12 and aws-ec2-assign-elastic-ip --version prints AWS EC2 Assign Elastic IP: 0.10.2 on 3.11 and 3.12 alike, which shows the version is still read from settings.conf at build time and at run time. The agent also searches for the two other configparser removals in 3.12, the readfp method and the filename attribute of ParsingError, and reports that this repository uses neither. It does not pretend to test the tool's real work, which needs the EC2 metadata service and AWS credentials.

Start it with one API call

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

{
  "agent": "claude",
  "input": "This project fails on Python 3.12 or later with \"ImportError: cannot import name 'SafeConfigParser' from 'configparser'\" (or AttributeError: module 'configparser' has no attribute 'SafeConfigParser'). Fix it. Find every use of SafeConfigParser, of the readfp method, and of ParsingError.filename, in setup.py, the package, scripts, tests and any vendored file such as versioneer.py. Reproduce the failure first: create a clean virtual environment on the newest Python available and run the project's install command, and record the traceback. Replace SafeConfigParser with ConfigParser, not RawConfigParser, and confirm by reading the config files involved that interpolation behaviour is unchanged. Replace readfp(f) with read_file(f). If the file that fails is vendored or generated (for example versioneer.py), regenerate it with a current release of its tool instead of editing it by hand, and say which. Decide whether Python 2 compatibility code around the import should stay by reading python_requires, classifiers and CI config; keep it only if Python 2 is still declared, and state the decision. After the change, install into clean environments on every supported Python you can get, run the test suite if there is one, and exercise at least one command or import that reads the config. Report anything you could not run and why. Say in the pull request whether a new release has to be published for users of the package index to get the fix. Open a pull request.",
  "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 two files, a few lines in each: the import and the constructor call in setup.py and in command_line_options.py. The description quotes the failing traceback from Python 3.12, then the same install succeeding, then the --version output on each interpreter tried. It records the two decisions, ConfigParser over the suggested RawConfigParser and the removal of the Python 2 import, each with its reason.

It ends with the point that is easy to forget. Merging fixes installs from the repository, and nobody installs from the repository. The README tells users to run pip install aws-ec2-assign-elastic-ip, and any source distribution already on the package index was built from the old setup.py and keeps failing on new Pythons until a new version is uploaded. The description says a release is required and that the version lives in settings.conf.

A reviewer checks the diff, which takes a minute, and then does the part the agent cannot: runs the tool once on an EC2 instance, since the repository has no tests and the sandbox has no instance metadata to answer it.

Run it on every GitHub issue labelled python-3.12

Maintainers usually learn about this from a user: an issue titled with the traceback, opened the week a distribution ships a new Python. Save the prompt above as an automation on the repository's Project with a github trigger on issues.labeled and labels set to the label you apply after reading such a report. Labelling the issue starts a run, with the issue placed after the saved prompt as third-party data. The saved prompt stays the only instruction, and the issue text is data for it to read.

Only labels applied by a repository owner, member or collaborator fire the trigger, which keeps strangers from starting runs. If you maintain many small packages, the same error tends to arrive in several of them within days. Each repository's Project needs its own automation, and each automation has its own max runs per hour.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Reproducing an install failure properly means a clean environment and the exact interpreter, and proving the fix means the same again for each supported version. Most laptops have one Python and a cluttered site-packages that hides this kind of problem. The agent builds each environment from nothing in its own sandbox, and the tracebacks and install logs are kept with the session, so the pull request's claim that 3.12 now installs is backed by output a reviewer can open.

It is also a fix where the obvious edits are subtly wrong. The interpreter recommends a class with different behaviour, and a two-branch import invites a third branch instead of a clean-up. An agent working from the prompt above checks the config file before choosing the class, reads the package metadata before touching the compatibility code, and writes both decisions down for the person who has to approve them.

Frequently asked questions

What replaces SafeConfigParser?

configparser.ConfigParser. On Python 3 they were the same class under two names, so the change is a rename with no difference in behaviour. Do not take the interpreter's hint to use RawConfigParser unless you want interpolation switched off.

pip install fails for a package I do not maintain. What can I do?

Look for a newer release first, since many projects have fixed this. If there is none, pin Python below 3.12 for that environment, or install from a fork that carries the fix. The agent can prepare that fork's pull request and, for your own project, change the requirement to point at it with a note about when to remove it.

The traceback is inside versioneer.py. Should I edit it?

No. That file is generated and vendored into the project, and old copies use both SafeConfigParser and readfp. Install a current release of versioneer and run its install step to regenerate the file, then commit the result. The agent does this and shows the tool version it used.

Will the fix still work on older Python 3 versions?

Yes. ConfigParser has had its current behaviour since Python 3.2, and read_file has existed as long. A project that supports 3.8 through 3.13 needs no conditional code after this change.

Why does the error come from setup.py and not from my code?

Because the package reads a config file while it is being built, often to find its own version number. The build tool executes setup.py, the import fails there, and the installer reports a build failure. It also means the fix reaches users only through a new release, since the published archive contains the old file.

More jobs like this: Fix a bug from an alert

Keep reading