# Fix ModuleNotFoundError: No module named 'imp' on Python 3.12

Yes. The agent finds the imp.load_source call Python 3.12 removed, replaces it with importlib, installs the package on 3.12 to prove it, and opens a pull request.

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

## What the agent does

Python 3.12 removed the `imp` module after a long deprecation, so any `import imp` now raises `ModuleNotFoundError: No module named 'imp'`. The common place is a `setup.py` that loads a version file with `imp.load_source`, which turns every `pip install .` into a build failure on 3.12. The run starts with that traceback, and the agent clones the repository into its own cloud sandbox.

In a Python tool like MJL85/natlas, `setup.py` lines 20 and 21 read `import imp` and `_version = imp.load_source('', 'natlas/_version.py')`, and `natlas/_version.py` holds `__version__ = "0.12.1"`. The rest of `setup.py` lists `graphviz`, `pydot`, `pysnmp>=4.2.5`, `pyparsing` and `netaddr>=0.7.14` as `install_requires`, the `natlas` package and a `natlas/natlas.py` script. The agent reproduces the failure with a 3.12 interpreter: `pip install .` stops inside the build backend with `ModuleNotFoundError: No module named 'imp'`, and `uv pip install .` reports the same line under `The build backend returned an error`.

The replacement is the `importlib.util` sequence the Python documentation gives for `imp.load_source`: `spec_from_file_location('natlas_version', 'natlas/_version.py')`, `module_from_spec`, then `spec.loader.exec_module`, after which `_version.__version__` reads as before. The agent keeps the version in one file rather than duplicating the string into `setup.py`. With that change `pip install .` on 3.12 completes and `importlib.metadata.version('natlas')` returns `0.12.1`.

It then runs the tool, because 3.12 removed more than `imp`. `natlas-cli.py` line 33 imports `LooseVersion` from `distutils.version`, which 3.12 also dropped, and uses it at line 223 to compare a module's `require_api` against `natlas.__version__`. The agent replaces that comparison in the same pull request, with `packaging.version.Version` as a new requirement or a tuple comparison of the dotted parts, and says which it chose. The module loader at line 122 uses `__import__` and needs no change.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "pip install . fails on Python 3.12 with ModuleNotFoundError: No module named 'imp'. Find every use of the imp module, replace imp.load_source in setup.py with importlib.util so the version is still read from its file, check the entry point for other modules Python 3.12 removed such as distutils, fix those too, install and run the package on Python 3.12 to confirm, and open a pull request that quotes the errors and explains each replacement.",
  "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

What comes back is a pull request touching `setup.py` and `natlas-cli.py`. Its description quotes the `imp` error from the build backend and the `distutils` error from the CLI, shows `pip install .` completing on 3.12 with the version read back, and lists the Python release that removed each module.

A reviewer checks that the version is still read from `natlas/_version.py` rather than copied, that an older interpreter still installs the package (the `importlib.util` calls exist there too), and that the version comparison keeps its meaning for dotted strings such as `0.12.1`. If `packaging` is added, it appears in `install_requires` and in whatever lock the project keeps.

The session keeps the failing install, the passing install and the CLI's output, so the reviewer can see the proof without a 3.12 interpreter at hand.

## Run it on every new Python version in CI

Create an automation in the Project with this prompt and a `github` trigger on `workflow_run.failure`. When a workflow fails after a Python version moves, such as `actions/setup-python` picking up 3.12, the run starts with the event after the prompt, reads the failing job's log, and opens the pull request.

A repository without CI can run the same prompt on a `schedule` trigger once a month, installing the package on the current Python release; a run that finds nothing to fix ends without a pull request and says so.

## Why run it on Rush?

The failure sits in `setup.py`, so it never shows in a test suite that already has the package installed. A Rush run installs from source on the Python version that broke it, in its own sandbox, and keeps the build backend's traceback next to the fix, so the pull request proves the install rather than asserting it.

Removed standard-library modules come in groups: `imp` and `distutils` left in the same release. One run fixes the install, runs the entry point, and finds the second removal before a user does. Several packages with the same `imp.load_source` pattern can be fixed at once, one run and one pull request each.

## Frequently asked questions

### Why not read the version with a regular expression instead of importing the file?

That works too, and the agent uses it when the version file has imports of its own that would run on `exec_module`. Here `_version.py` is one assignment, so loading it with `importlib.util` keeps `__version__` as the single source and changes the least.

### Does this break installs on older Python versions?

No. `importlib.util.spec_from_file_location` and `module_from_spec` exist on every Python 3 release the package can run on, so the same `setup.py` installs on an older interpreter and on 3.12 alike.

### What replaces distutils.version.LooseVersion?

`packaging.version.Version` is the standard replacement and is what pip itself uses. It adds `packaging` to the requirements; for a project that wants no new dependency, the agent compares tuples of integers from the dotted string instead and says so.

### Will it also move the project to pyproject.toml?

Not in this pull request. The ask is the 3.12 failure, and a packaging migration is its own change. The agent notes in the description that `setup.py` could become `pyproject.toml` with a dynamic version if the maintainer wants that next.

## The same job on other stacks

- [Fix "ModuleNotFoundError: No module named 'imp'" on Python 3.12](https://getrush.ai/run/fix-no-module-named-imp/python/)

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

- [Fix "ModuleNotFoundError: No module named 'kafka.vendor.six.moves'" on Python 3.12](https://getrush.ai/run/fix-no-module-named-kafka-vendor-six-moves/kafka-python/)
- [Fix "ModuleNotFoundError: No module named 'moviepy.editor'"](https://getrush.ai/run/fix-no-module-named-moviepy-editor/moviepy/)
- [Fix "ModuleNotFoundError: No module named 'pip.req'" in setup.py](https://getrush.ai/run/fix-no-module-named-pip-req/pip/)
- [Fix "ModuleNotFoundError: No module named 'pipes'" on Python 3.13](https://getrush.ai/run/fix-no-module-named-pipes/pipes/)
- [Fix "ModuleNotFoundError: No module named 'pkg_resources'" after a setuptools upgrade](https://getrush.ai/run/fix-no-module-named-pkg-resources/setuptools/)

## 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/)
