# Fix "ModuleNotFoundError: No module named 'pip.req'" in setup.py

Yes. An agent removes the pip.req import from setup.py, reads the requirements with code that needs no pip internals, and installs the package to prove it.

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

## What the agent does

For years a common trick in `setup.py` was `from pip.req import parse_requirements`, used to turn `requirements.txt` into `install_requires`. pip never offered that function as an API. Release 10 moved its internals under `pip._internal`, and the import has failed ever since with `ModuleNotFoundError: No module named 'pip.req'`, or `ImportError: No module named pip.req` on Python 2. Projects that were not touched after that release still carry the line, and anyone who installs them from source hits it before a single file is copied.

evidens/json2csv, a converter from JSON to CSV, is one of them. Line 1 of its `setup.py` is the import, and line 15 builds `install_requires` with `parse_requirements('requirements.txt', session=uuid.uuid1())`, a random UUID passed where pip once wanted a session object. `requirements.txt` holds one line, `unicodecsv==0.9.0`. Running `python setup.py --version` on Python 3.12 gives the error in the title. Running `pip install .` with pip 26.3 gives a different one, `ModuleNotFoundError: No module named 'pip'`, because pip now builds a project in an isolated environment that contains setuptools and not pip itself. The two messages have one cause.

The agent reproduces both, then replaces the import with something that cannot break again. The usual patch, switching to `pip._internal.req`, is the one it avoids: that module has changed its signatures several times and is not importable inside an isolated build at all. A dozen lines of plain Python read the file, skip comments and option lines, and return the requirement strings. Where the project is ready for it, the agent proposes the declarative form instead, a `pyproject.toml` whose `dependencies` are declared `dynamic` and read from `requirements.txt` by setuptools. It also fixes what the first successful parse reveals. This `setup()` call passes `modules=['json2csv']`, an option setuptools reports as unknown and ignores, where `py_modules` was almost certainly meant, so the module the package is named after was never declared.

Then it installs the package in a clean environment, which is the step that shows whether the title's error was the only one. Here it is not. With `setup.py` repaired, `pip install .` on Python 3.12 stops at the pinned dependency: `unicodecsv==0.9.0` has a Python 2 `setup.py` of its own and fails with a `SyntaxError`, while the current unicodecsv release installs. And the project's `tests.py`, run on 3.12, ends with 11 errors out of 14 tests, starting with `NameError: name 'reduce' is not defined` on line 72 of `json2csv.py`. The agent fixes the packaging, reports the rest with file and line, and ports the Python 2 code only if the request says to.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Installing this project from source fails with ModuleNotFoundError: No module named 'pip.req' (or No module named 'pip' when installed with a recent pip). Read setup.py, setup.cfg, pyproject.toml, MANIFEST.in and every requirements file, and report each import from pip or pip._internal with file and line, and how the parsed requirements are used. In a clean virtual environment on the newest Python the project claims to support, run python setup.py --version and pip install . separately and save both tracebacks, with the pip and setuptools versions. Remove every import of pip from the packaging code. Do not replace it with pip._internal, with a try/except around several pip import paths, or with a vendored copy of pip's parser. Read the requirements file with plain Python that skips blank lines, comments and lines starting with a dash, resolving the path relative to setup.py; or, if the project already has a pyproject.toml with a setuptools build backend, declare dependencies as dynamic and read them from the file there. Keep the set of install requirements identical and show the before and after lists. Fix any setup() arguments that setuptools reports as unknown or deprecated, and say what each one was meant to be. Then run pip install . and pip install -e . in fresh environments, import the package, and run the test suite. If a pinned dependency itself cannot be installed on this Python, or the tests fail for reasons unrelated to packaging, report each with its error and the smallest change that would fix it, but keep those changes out of this pull request unless they are needed for the install to finish. Open a pull request with the tracebacks, the requirement lists and the install 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 changes `setup.py`, or replaces part of it with a `pyproject.toml` section, and nothing in the package's own code. The description shows the two original tracebacks side by side, since users report either one, and the list of install requirements before and after the change so a reviewer can see they are the same strings.

Next comes the output of a real install in a fresh environment, and that is the line to check: the package builds, installs and imports. If setup arguments were corrected, each is listed with what it did before, which for a mistyped option is usually nothing, and what it does now. A reviewer confirms that the files they expect are in the built distribution.

The last section is the honest remainder. An unmaintained project rarely has one problem, and the agent lists what it met after the packaging worked: pins that no longer install, tests that fail on current Python, each with the error and a proposed fix. Those are offered as follow-ups, to be requested in a message to the same session or left for later.

## Run it on every GitHub issue labelled install-broken

Reports of this error come from users, as issues that quote one of the two tracebacks. Give the Project an automation with a `github` trigger on `issues.labeled`, filtered to a label such as `install-broken`, and a prompt that tells the agent to reproduce the install command from the issue in a clean environment and repair the packaging if the failure is in `setup.py`. The issue text is added after the prompt. Only an issue opened by an owner, member or collaborator fires the trigger, so for an outside report a maintainer opens and labels a tracking issue of their own.

An organisation with many small libraries from the same era can sweep them instead. A code search for `pip.req` gives the list, and the request above, sent once per repository with an API key, turns it into pull requests. Runs past the key's `max_concurrent` wait as `queued`.

## Why run it on Rush?

Packaging fixes are verified by installing, in an environment that has never seen the project, with a current pip. That is exactly what a maintainer's machine is not: it has the package already installed in development mode, an older pip, and setuptools on the path. The isolated-build variant of this error, the one that names `pip` instead of `pip.req`, does not appear there at all. A sandbox starts empty, so the agent sees both messages, and the install that proves the fix is the same install a new user will run.

The run also keeps going past the first success. Each step, `setup.py` parsing, dependency resolution, the build, the import, the tests, is executed and its output kept in the session. On an old project the title's error is the outermost of several, and a pull request that names the ones underneath saves a second and a third round of bug reports.

## Frequently asked questions

### Can I change the import to pip._internal.req instead?

It works on some pip versions and breaks on others, because the function's location and the shape of what it returns have both changed more than once. Inside an isolated build pip is not importable at all, so `pip install .` fails either way. Reading the file directly has no such dependency.

### Why does pip install say No module named 'pip' and not 'pip.req'?

A recent pip builds your project in a temporary environment that holds only the build requirements, by default setuptools. pip is not in it, so the failure happens one name earlier. Running `python setup.py` directly, where pip is importable, shows the `pip.req` form.

### Should install_requires be read from requirements.txt at all?

It is a matter of taste for a small project, and the agent keeps your current behaviour by default. Loose ranges in the package metadata and exact pins in a requirements file is the more common split for a library. Ask for that in the request if you want it changed.

### Will the agent downgrade pip to make the old code work?

No. Your users install with whatever pip they have, and pinning the installer inside a project is not possible in any case. The request above removes the dependency on pip's internals instead.

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

- [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/)
- [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/)
- [Fix "ModuleNotFoundError: No module named 'telnetlib'" on Python 3.13](https://getrush.ai/run/fix-no-module-named-telnetlib/telnetlib/)

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