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

Yes. An agent replaces the removed imp module with importlib, then finds the tools around your code that import imp too, and proves the install on Python 3.12.

[Run this on Rush](https://cloud.getrush.ai/run/fix-no-module-named-imp/python/). 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, so any file that says `import imp` stops at that line. The agent starts by finding where the import lives, because that decides who sees the error. In a library like alvarosg/pynverse it is not in the package at all. It is in `setup.py`: line 3 is `import imp`, and line 5 is `version = imp.load_source('pynverse.version', 'pynverse/version.py')`, which reads the one-line `pynverse/version.py` (`git_revision = '0.1.4.6'`) so the build can use it as the version. The README tells users to run `pip install git+https://github.com/alvarosg/pynverse.git`, and that command builds from source. So on 3.12 nobody gets as far as importing the library: the build backend runs `setup.py` to ask for its requirements and the traceback ends at line 3.

The agent creates a Python 3.12 environment in the sandbox, runs the install and keeps the traceback as the starting evidence. Then it replaces the call. The direct equivalent of `imp.load_source` is three lines from `importlib.util`: `spec_from_file_location` with the same name and path, `module_from_spec`, then `spec.loader.exec_module`. One difference is that `load_source` also put the module in `sys.modules` and the three lines do not; for a file that only holds a version string that changes nothing, and the agent says so instead of adding the registration by habit. Where the file is this simple it also offers the smaller option of not executing it at all, by reading the version with setuptools' `attr:` support in `setup.cfg`.

Fixing the line you searched for is rarely the end, and the agent keeps going until a clean install and a test run both pass. This repository shows why. Its `setup.py` sets `test_suite='nose.collector'` and lists `nose>=1.0` under `setup_requires` and `tests_require`. nose is long unmaintained, and its `nose/importer.py` opens with `from imp import find_module, load_module, acquire_lock, release_lock`, so `python -m nose` dies on 3.12 with the same error from a file you do not own. A current setuptools also no longer has the `test` command those keywords configured; it warns that `test_suite` and `tests_require` are unknown options and answers `python setup.py test` with `invalid command 'test'`. The agent removes the three nose keywords and runs the suite in `pynverse/tests/test_inverse.py` with pytest, which needs no change to the test file because its 12 tests are plain functions using `numpy.testing` assertions.

Last, it searches the whole repository for the other spellings of the problem: `from imp import`, `imp.reload` (now `importlib.reload`), `imp.find_module` (now `importlib.util.find_spec`) and `imp.new_module` (now `types.ModuleType`), including vendored code and scripts under `examples/`. It also lists what it noticed and left alone. Here that is the classifier list, which stops at Python 3.5, and the `description-file` key in `setup.cfg`, which setuptools reports as a deprecated dash-separated name.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Installing or importing this project on Python 3.12 fails with \"ModuleNotFoundError: No module named 'imp'\". Create a Python 3.12 virtual environment, run the install the README documents and the test command the project uses, and save each traceback. Find every use of the imp module: import imp, from imp import, in setup.py, the package, scripts, examples and vendored code. Replace each with its importlib equivalent (imp.load_source with importlib.util.spec_from_file_location, module_from_spec and exec_module; imp.reload with importlib.reload; imp.find_module with importlib.util.find_spec; imp.new_module with types.ModuleType) and say for each call whether the old one also registered the module in sys.modules and whether that matters here. Then check the tools the project depends on: if the test runner or a build requirement itself imports imp (nose does), replace it with one that works on 3.12 and say what you changed in setup.py, setup.cfg or the CI workflow. Do not change the library's behaviour or its public API. Finish with a clean install from a fresh environment and the full test suite on Python 3.12, and on the oldest Python version the project still claims if it is available. Open a pull request with the before and after output, and list 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 with a small diff: `setup.py` loses `import imp` and gains the importlib lines (or the version moves to `setup.cfg`), the nose keywords go, and a test requirement on pytest appears wherever the project keeps such things. The description holds the original traceback, the output of the install in a fresh 3.12 environment after the change, and the test summary. If the repository has a CI workflow, the pull request adds 3.12 to its version matrix so the fix cannot regress unnoticed; a repository with no workflow, like this one, gets a note saying nothing runs the tests automatically.

A reviewer checks two things. First, that the version the build reports is still the string in `pynverse/version.py`, which the description shows by printing `python setup.py --version` before and after. Second, that the test runner swap did not drop tests: the description gives the number collected by pytest next to the number of `def test_` functions in the file. The section of things left alone tells them what the next pull request could be.

## Run it on every failed workflow run

This error usually arrives when a CI image or a base image moves to Python 3.12 and a job that passed yesterday fails today. Add an automation on the Project with a `github` trigger on `workflow_run.failure` and the prompt above. The failed run is appended after the prompt as event text, so the agent sees which workflow and branch failed. The prompt stays the only instruction: if the failure is not this error, the agent reports what it found and opens nothing.

For a fleet of small libraries, a schedule works better than waiting for each to break. A `schedule` trigger with a weekly `cron` can run the same prompt in each Project, and the automation's max runs per hour keeps a burst of failures from starting more work than you want to review.

## Why run it on Rush?

The fix needs an interpreter most laptops do not have selected, and it needs a clean one. A developer's machine often has an old virtual environment in which setuptools, nose or a cached wheel hides the second and third failure. The sandbox starts empty, so the install the agent runs is the install a new user gets, and the output is kept with the run instead of scrolling away in a terminal.

The error message names one module, but the work is a chain: your `setup.py`, then the test runner, then a command setuptools removed. Each link only shows after the one before it is fixed. An agent that keeps installing and testing until both pass walks the whole chain in one run, and the pull request shows each step with the command that exposed it.

## Frequently asked questions

### What replaces imp.load_source?

`importlib.util.spec_from_file_location(name, path)`, then `importlib.util.module_from_spec(spec)`, then `spec.loader.exec_module(module)`. Add `sys.modules[name] = module` before the last call only if other code imports the module by that name afterwards. For a version file, reading the value without executing the file is simpler still.

### Can I just pin Python below 3.12?

You can, and the error goes away for as long as that interpreter is available on your images. It does not help users who install your package on their own 3.12. The warning was there before the removal: on 3.11, `import imp` raises a DeprecationWarning that says the module is slated for removal in Python 3.12, which Python shows when warnings are enabled.

### The traceback points into site-packages, not my code. Will the agent patch the dependency?

No. It does not edit installed packages. It finds which of your requirements pulls in the file that imports `imp`, checks whether a newer release has dropped the import, and upgrades or replaces that requirement. nose has no such release, which is why the agent moves the tests to another runner.

### Does the fixed code still work on older Python versions?

The `importlib.util` functions the agent uses exist on every Python 3 release still supported, so the same `setup.py` installs on old and new interpreters. If the project still claims Python 2.7, as this one's classifiers do, the agent reports that the claim and the fix cannot both hold and asks which to keep in the pull request description.

## More Python jobs

- [Turn GitHub Issues into Pull Requests for a Python Project](https://getrush.ai/run/github-issue-to-pr/python/)
- [Migrate a Python Project from flake8 to Ruff](https://getrush.ai/run/migrate-flake8-to-ruff/python/)
- [Migrate a Python Project from setup.py to pyproject.toml](https://getrush.ai/run/migrate-setup-py-to-pyproject/python/)

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

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

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