# Fix "ModuleNotFoundError: No module named 'pkg_resources'" after a setuptools upgrade

Yes. An agent finds every pkg_resources import, replaces each with importlib.resources or importlib.metadata, and proves the server still serves its files.

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

## What the agent does

The agent starts from the dependency file, because this error now comes from a package that used to be everywhere. In dcoles/golink, a small aiohttp server for go/ links, `requirements.txt` lists `aiohttp`, `aiohttp_jinja2`, `attrs`, `jinja2`, `setuptools` and `pymongo[srv]`, none of them pinned. The README's install steps are a virtual environment, `pip3 install -r requirements.txt` and `python3 -m golink.webapp --auth anonymous --database golinks.sqlite`. Following them today installs setuptools 84.2.1, and the first line of the server's start-up, `import pkg_resources` on line 5 of `golink/webapp.py`, raises the error in the title. The one use is on line 41: `app.router.add_static('/+static', pkg_resources.resource_filename('golink', 'static'))`, which turns the package name into the directory of CSS and JavaScript the pages load.

Then it finds the release that changed. Installed one version at a time, setuptools 81.0.0 still imports `pkg_resources`, with a `UserWarning` that it is deprecated as an API, is slated for removal as early as 2025-11-30, and that callers should refrain from using it or pin to Setuptools below 81. Setuptools 82.0.0 has no `pkg_resources` package at all. So a project that imports it breaks the moment its environment resolves setuptools 82 or later, and a `setuptools` line with no upper bound guarantees that on the next clean install. An environment that ships no setuptools, such as one made with uv, fails the same way on any version.

The replacement is in the standard library, so the fix adds no dependency. `importlib.resources.files('golink') / 'static'` names the same directory, and `str()` of it is what `add_static` takes. The agent makes that two-line change, keeps the `setuptools` line out of `requirements.txt` unless something else needs it, and then runs the project's own checks: the server module imports, the resolved path is the package's `static` directory in the checkout, and the test suite under `golink/test`, which builds the aiohttp application with a stand-in database and authenticator, passes with every test green. Where a project uses other parts of the old API, the agent maps each one: `iter_entry_points` becomes `importlib.metadata.entry_points`, `get_distribution(name).version` becomes `importlib.metadata.version`, and `resource_string` becomes `files(...).joinpath(...).read_bytes()`.

It also says what a quick fix would cost. Pinning `setuptools<82` keeps the import working, but every install then prints the deprecation warning, and the pin has to be carried in every environment that runs the code, including the one a `python -m venv` makes on a current Python, which starts without setuptools. The pull request recommends the rewrite and offers the pin only as a stopgap for a dependency the team cannot patch.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This project fails to start with \"ModuleNotFoundError: No module named 'pkg_resources'\". Find every import of pkg_resources in the project's own code and list each use with file, line and which API it calls (resource_filename, resource_string, iter_entry_points, get_distribution, working_set or another). Read the dependency files and say how setuptools gets into the environment and which version a clean install resolves today. Find by installing them the last setuptools release that ships pkg_resources and the first that does not, and quote the warning the last one prints. Replace each use with its importlib.resources or importlib.metadata equivalent, keeping the same paths, strings and versions, and remove any setuptools requirement the code no longer needs. Prove the result: import the entry module in a fresh environment, show that each resolved path or value equals what the old call returned on the last setuptools that has pkg_resources, and run the project's tests. Open a pull request with the version findings, the before-and-after values and the test output.",
  "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 edits the import and the one call in `golink/webapp.py`, and the dependency file if `setuptools` was only there for `pkg_resources`. Its description names the setuptools release that dropped the package and the one before it, quotes the deprecation warning, and shows the `static` path the old and new calls return side by side. Below that is the test run from the sandbox and the start-up log of the server on the new code.

A reviewer checks three things: that every `pkg_resources` use found by search is in the table, that the replacement for each one is the equivalent named in the pull request and not a different API, and that the test suite ran on the branch. If the project is published as a package, the reviewer also confirms the files the old call pointed at are still included in the wheel, since `importlib.resources` reads them from the installed package the same way.

What the run does not prove is production: the sandbox has no MongoDB, so the `mongodb` backend of this server is imported but not exercised. The pull request says so, and the deploy that follows is the check.

## Run it on every failed deploy or CI run

The error appears the first time an environment is rebuilt after setuptools 82 reached the index, so it tends to surface in a deploy log or a CI job rather than from a code change. A job in GitHub Actions or any CI system can start the run itself: add a step on failure that sends the job's log tail to a Rush `webhook` trigger's URL with the trigger's secret in `Authorization: Bearer`, and the automation's prompt tells the agent to look for the module name in the log before anything else.

A service like Sentry, which signs its own deliveries and cannot add that header, needs a handler on your server that receives the event and forwards it. Have the handler send an `Idempotency-Key` header to the trigger's URL built from the issue id, so a retried delivery starts nothing new, and keep the automation's runs per hour low, since one broken environment produces many identical events.

## Why run it on Rush?

This failure depends on which setuptools a machine happens to have. A laptop with an environment from last year still imports `pkg_resources` and shows nothing wrong. A Rush sandbox starts clean, resolves the dependency files as a new deploy would, and hits the error on the first import, and the agent can then install each setuptools release in turn to find the exact boundary, with every command and its output kept in the session.

The rewrite is mechanical but the proof is not: the value of a path or a version string has to match what the old API returned, for each call, and the project's tests have to run on the branch. The pull request carries those comparisons and the test run, so the reviewer judges evidence rather than reading the diff and hoping.

## Frequently asked questions

### Why did this start failing without any change to my code?

Setuptools removed the `pkg_resources` package in release 82.0.0, after deprecating it with a warning that named a removal date. Any environment that resolves setuptools without an upper bound now gets a version without it, and environments made by tools that do not install setuptools at all never had it.

### Can I just pin setuptools below 82?

Yes, as a stopgap, and the pull request says where. It keeps the deprecation warning on every install, it has to be repeated in every environment that imports the code, and it does nothing for an environment that ships no setuptools. The standard-library replacements have none of those costs.

### What replaces resource_filename and iter_entry_points?

`importlib.resources.files(package) / name` gives the path a package file has, and `importlib.metadata.entry_points(group=...)` lists plugins registered under a group. `importlib.metadata.version(name)` replaces `get_distribution(name).version`. All three are in the standard library of the Python versions supported today.

### What if the import is inside a library I depend on, not my code?

Then the agent finds the library's first release that stopped importing it and upgrades to that, with the before-and-after import shown. If no such release exists, it pins setuptools below 82 for that project and says in the pull request which dependency forces it.

## More setuptools jobs

- [Fix "Support for the test command was removed in Setuptools 72"](https://getrush.ai/run/fix-setup-py-test-command-removed/setuptools/)
- [Fix "error in setup command: use_2to3 is invalid" in setup.py](https://getrush.ai/run/fix-setuptools-use-2to3-error/setuptools/)
- [Fix "No module named 'pkg_resources'" by Migrating to importlib.resources](https://getrush.ai/run/migrate-pkg-resources-to-importlib-resources/setuptools/)

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

- [Fix "ModuleNotFoundError: No module named 'plotly.plotly'" after plotly 4](https://getrush.ai/run/fix-no-module-named-plotly-plotly/plotly/)
- [Fix "ModuleNotFoundError: No module named 'slackclient'" by Porting a Bot to slack_sdk](https://getrush.ai/run/fix-no-module-named-slackclient/slackclient/)
- [Fix "ModuleNotFoundError: No module named 'smtpd'" on Python 3.12](https://getrush.ai/run/fix-no-module-named-smtpd/smtpd/)

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