# Fix "AttributeError: module 'locale' has no attribute 'format'" on Python 3.12

Yes. An agent replaces locale.format() with locale.format_string(), then checks what grouping does to output the tests only ever see in the C locale.

[Run this on Rush](https://cloud.getrush.ai/run/fix-locale-format-attribute-error/locale/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Python 3.12 removed `locale.format()`. It had been deprecated since 3.7, and through 3.11 a call emitted `DeprecationWarning: This method will be removed in a future version of Python. Use 'locale.format_string()' instead.` The replacement takes the same arguments in the same order, so `locale.format("%0.1f", x, grouping=True)` becomes `locale.format_string("%0.1f", x, grouping=True)` and returns the same text. The one difference runs in the safe direction: the old function raised `ValueError` for anything but a single `%` specifier, and the new one accepts a whole format string. The edit is small. The job is finding out why nobody saw the warning for five releases, and what the formatted text looks like outside the test suite.

twoolie/NBT, a Minecraft file parser, shows both. Its `examples` directory has four scripts that call `locale.setlocale(locale.LC_ALL, '')` and then format numbers. Two of them, `biome_analysis.py` and `block_analysis.py`, already use `locale.format_string`. The other two do not: `print_results` in `mob_analysis.py` and in `chest_analysis.py` each call `locale.format("%0.1f", ..., grouping=True)` three times, once per coordinate, and join the results as `"%s at %s,%s,%s"`. The agent finds all six calls with a search for `locale.format(` across the repository, and confirms that nothing under `nbt/` itself touches the module.

Then it reads how the examples are tested. `tests/examplestests.py` runs each script in a subprocess, and `runScript` sets `env['LC_ALL'] = 'C'` before it does. The assertions compare lines such as `Chicken at 107.6,88.0,374.5`. The workflow in `.github/workflows/tests.yaml` runs `python tests/alltests.py` on Python 3.7, 3.8 and PyPy 3.9, so no job has ever reached 3.12, and a warning written to a subprocess's stderr fails nothing. The agent adds 3.12 and the current release to the matrix so the removal shows up as a red job, which also proves the fix when it turns green.

The `LC_ALL=C` line hides a second problem that the agent reports instead of fixing silently. `grouping=True` inserts the locale's thousands separator, and the C locale has none. In a locale that groups with a comma, a coordinate past 999 prints as `1,234.5`, and a line built with `%s,%s,%s` reads `Chicken at 1,234.5,64.0,-2,010.3`, which no longer splits into three numbers. The scripts call `setlocale` with the user's own locale, so that is what a person running them on a large world would get. The agent keeps the behaviour identical in the fix and lists the choice separately: keep grouping and change the separator between coordinates, or drop grouping for coordinates, which are positions and not quantities.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails on Python 3.12 with \"AttributeError: module 'locale' has no attribute 'format'\". Find every call to locale.format( in the repository, including examples, scripts and documentation snippets, and list each with its format string and its arguments. Replace each with locale.format_string(), keeping the grouping and monetary arguments exactly as they were, and say for each call whether the format string held exactly one % specifier. Do not change what any call prints. Then read how these code paths are tested: find where the tests set LC_ALL, LANG or call setlocale, and say which locale every assertion actually runs under. For each call that passes grouping=True, work out what the output looks like in a locale with a thousands separator and in one with a decimal comma, and flag any output where the separator collides with punctuation the surrounding format already uses, such as comma-separated coordinates or CSV columns. Report those as findings with a proposed change, but keep them out of the fix commit. Add Python 3.12 and the newest stable Python to the CI matrix if they are missing, run the test suite on the oldest and newest versions the project claims to support, and include the commands and their output. Open a pull request with the replacement, the CI change and the findings.",
  "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 with two commits. The first changes the six calls in the two example scripts and nothing else. The second extends the Python versions in the workflow file. The description has a table with one row per call: file, line, format string, arguments, and whether the format string would have been legal for the old function. It quotes the test command and what it printed on the oldest and newest interpreters, and it says in which locale those tests ran.

Below that is a findings section that changes no code. For NBT it would hold the grouping collision described above, with one example line per locale, and a note that the two scripts already using `format_string` pass no grouping argument, so the four examples are not consistent with each other. A reviewer checks the diff, which is mechanical, and then decides the finding. That decision is about what the scripts should print, and whoever maintains them makes it.

The reviewer also looks at the matrix change. A project whose `setup.py` classifiers stop at an old Python may want the new versions as allowed failures first. The agent states which classifiers it found and leaves them alone unless the tests pass on the versions it added.

## Run it on every failed workflow run

Removals like this one arrive on the day a runner image or a base image moves to a new Python. Put an automation on the Project that holds the repository, with a `github` trigger on `workflow_run.failure` and the prompt above. Rush adds the event after the prompt, so the agent knows which workflow and branch failed. Runs from forks and from bot senders never fire the trigger.

One interpreter upgrade can fail every workflow in a repository at once. Set the automation's max runs per hour to a small number so that one upgrade produces one run, and add a line to the saved prompt telling the agent to stop with a one-paragraph answer when the failing log does not contain this `AttributeError`.

## Why run it on Rush?

The interesting part of this fix depends on the locale of the machine that runs it. A laptop set to one language shows one output and hides the rest. In its sandbox the agent can generate other locales, run the same script under each with `LC_ALL` set, and keep the printed lines with the session, so the pull request shows the collision instead of describing it.

It also needs several interpreters side by side: the oldest the project claims, the last one that still warned, and the first one that fails. Installing three Pythons to change six lines is the kind of setup people skip. A run does it every time, and its record shows each command and what came back, which is what lets a reviewer trust a matrix change they did not test themselves.

## Frequently asked questions

### Is locale.format_string() a drop-in replacement for locale.format()?

For any call that worked before, yes. It takes the format, the value, `grouping` and `monetary` in the same positions and returns the same string. It is also less strict: a format with text around the specifier, or with several specifiers and a tuple of values, raised `ValueError` in the old function and works in the new one.

### Why did I never see the deprecation warning?

Python hides `DeprecationWarning` by default unless the code that triggers it runs in `__main__`, and many test runners capture stderr from subprocesses. In NBT the examples run as subprocesses of the test suite, so the warning went to a pipe that the tests print but never assert on.

### Does the fix still work on old Python versions?

`locale.format_string` has existed since long before the old function was deprecated, including on Python 2.7, so the replacement does not narrow the versions a project supports. The agent runs the tests on the oldest interpreter it can install to confirm that for your code.

### Will the agent change my output to remove grouping?

No. The fix commit keeps every argument as it was, so the output is byte for byte the same in every locale. Anything that would change what users see is reported as a finding with examples, for you to accept or decline.

## More jobs like this: Fix a bug from an alert

- [Fix "warning: Logger.warn/1 is deprecated. Use Logger.warning/2 instead" in an Elixir Library](https://getrush.ai/run/fix-logger-warn-is-deprecated/elixir/)
- [Fix "module declares its path as: github.com/sirupsen/logrus" in a Go Project](https://getrush.ai/run/fix-logrus-module-path-mismatch/logrus/)
- [Fix "ImportError: lxml.html.clean module is now a separate project lxml_html_clean"](https://getrush.ai/run/fix-lxml-html-clean-separate-project-import-error/lxml/)
- [Fix "TypeError: marked is not a function" After Upgrading marked](https://getrush.ai/run/fix-marked-is-not-a-function/marked/)
- [Fix "cannot import name 'soft_unicode' from 'markupsafe'"](https://getrush.ai/run/fix-markupsafe-soft-unicode-import-error/markupsafe/)
- [Fix "AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'" on Matplotlib 3.9](https://getrush.ai/run/fix-matplotlib-cm-get-cmap-attribute-error/matplotlib/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
