# Fix "ModuleNotFoundError: No module named 'kafka.vendor.six.moves'" on Python 3.12

Yes. An agent finds every place kafka-python 2.0.2 is pinned, moves each to a release that imports on Python 3.12, and proves it by importing the client there.

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

## What the agent does

kafka-python 2.0.2 ships its own copy of the `six` compatibility library under `kafka.vendor`. That copy provides `six.moves` through an import hook written against an importer interface Python 3.12 no longer honours, so on 3.12 the first `import kafka` fails inside the package, at line 9 of `kafka/codec.py`: `from kafka.vendor.six.moves import range`, then `ModuleNotFoundError: No module named 'kafka.vendor.six.moves'`. The same pinned version imports without complaint on Python 3.11. Nothing is wrong with your code or your broker settings. The interpreter moved and the pin did not.

hoangsonww/End-to-End-Data-Pipeline shows why this is easy to walk into. The pipeline runs on Python 3.10 throughout: `PYTHON_VERSION: "3.10"` in `.github/workflows/cicd-pipeline.yml`, `FROM python:3.10-slim` in `kafka/Dockerfile`, an Airflow 2.7.3 image tagged `python3.10`, and `target-version = ["py310"]` in `pyproject.toml`. `kafka-python==2.0.2` is pinned in four places: `requirements.txt`, `airflow/requirements.txt`, `kafka/Dockerfile` and `spark/Dockerfile`. Six modules use the client, among them `kafka/producer.py`, three consumers under `storage/` and an Airflow DAG, and every one of them imports it inside a function.

That last detail decides how the failure shows up. `tests/test_kafka_producer.py` imports only `generate_event` from the producer, which never touches the client. With Python 3.12 and kafka-python 2.0.2 those tests pass, while calling `create_producer()` in the same environment raises the error in the title. Lint does not import anything either. A pull request that bumps the Python version would go green in CI and fail when the producer container starts. So the agent does not stop at the test suite. It lists each function-level `from kafka import` with its file and line and calls an import of every name used (`KafkaProducer`, `KafkaConsumer`, `KafkaAdminClient`, `TopicPartition`, `kafka.errors`) on the target Python.

Then it moves the pin. kafka-python 2.0.3 is the smallest change that imports on Python 3.12, and the current release, a major version later, imports too. The agent applies one version to all four places so the images and the requirements files cannot drift apart, and adds a test that imports the client at module level so CI covers it from now on. It also reports what else blocks the Python bump: in this repository a 3.12 install of `requirements.txt` has to build `snowflake-connector-python==3.6.0` from source, and stops there on a machine without a C++ compiler. The agent does not build the Docker images, since the sandbox has no Docker, and says in the pull request that the image builds are left to CI.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "On Python 3.12 this repository fails with ModuleNotFoundError: No module named 'kafka.vendor.six.moves'. Find every place the Kafka client is pinned: requirements files in every directory, pyproject.toml, setup.py, Pipfile, and pip install lines inside Dockerfiles and CI workflows. Report each file, line and version, and whether both kafka and kafka-python are listed, since they install the same top-level package. Report every place a Python version is set (CI workflow, Dockerfile base images, pyproject target versions, runtime files). List every import of kafka in the code with file and line, and mark the ones that are inside a function body, because a test run will not execute those. In a clean Python 3.12 virtual environment install only the pinned client and import every kafka name the code uses; save the traceback. Then find the smallest newer kafka-python release that imports on 3.12 and also check the current release; report both and read the changelog between the pinned version and each for changes to the producer, consumer and admin APIs this code calls. Do not switch to a different client library. Apply one version to every pinned location so they agree. Add a test that imports, at module level, every Kafka client name the code uses, so a test run fails when the client cannot be imported. Install the full requirements on 3.12 and report any other package that fails to install or import, without fixing unrelated ones. Do not build Docker images. Open a pull request listing every changed pin, the import results on each Python version, and what still blocks a move to 3.12.",
  "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 one version string in each place the client was pinned, plus one new test file. In a multi-service repository like the one above that means two requirements files and two Dockerfiles moving together. The description has a table of location, old version and new version, and the result of importing each client class on the old Python and on 3.12, before and after.

A reviewer checks two things the diff cannot show. First, that the chosen version is the one the description argues for: the smallest step that imports, or the current release with the changelog read for the calls this code makes, such as `value_serializer`, `acks` and `linger_ms` on the producer. Second, the section on what the agent could not verify. It did not connect to a broker and did not build images, so the first run of the producer and consumer containers in CI or staging is still the real test of delivery.

The list of other 3.12 blockers is information, not a change. Packages that fail to install on the new interpreter are named with their error, and each is left for its own pull request.

## Run it on every pull request labelled python-upgrade

The moment to catch this is when someone proposes the interpreter bump, before it merges on the strength of green tests that never import the client. Give the Project an automation with a `github` trigger on `pull_request.labeled`, filtered to a label such as `python-upgrade`, and a prompt that tells the agent to check out the pull request's branch, import every third-party package the code uses on the new Python, including the imports that sit inside functions, and report what fails. The pull request is added after the prompt as event text, and a pull request from a fork does not fire the trigger.

If you would rather sweep several services at once, send the request above per repository with an API key. Each run starts in its own sandbox with its own interpreter, and runs beyond the key's `max_concurrent` wait as `queued` until earlier ones finish.

## Why run it on Rush?

This bug hides from the checks most teams already run. Imports inside functions are skipped by tests that do not call those functions and by linters that read text, so CI reports success for an environment in which the service cannot start. Finding it means installing the pinned client on the new interpreter and executing the imports. An agent does that in a sandbox, where adding a second Python next to the first is routine, and the session keeps the traceback and the passing import as output anyone can read.

The fix also has to land in several files at once, some of them Dockerfiles that no dependency bot reads. An agent that searches the whole repository for the version string, changes every hit, and lists them in one table removes the usual failure of this kind of upgrade: one image left on the old pin.

## Frequently asked questions

### Which kafka-python version works on Python 3.12?

In a check on Python 3.12, kafka-python 2.0.2 failed to import and 2.0.3, 2.1.0 and the current release imported. The fork published as kafka-python-ng also imported. The agent verifies the version it proposes on your target interpreter and does not rely on a list.

### Can I install six to fix it?

No. The failing import is of `kafka.vendor.six`, the copy bundled inside kafka-python 2.0.2, and installing the separate `six` package does not replace that copy. The fix is a kafka-python release whose bundled copy works on 3.12.

### Why do my tests pass on Python 3.12 when the service crashes?

Because nothing in the test run imports the client. When `from kafka import KafkaProducer` sits inside a function, the import happens only when that function is called. The request asks the agent to mark those imports and to add a test that imports the client at module level.

### My requirements list both kafka and kafka-python. Does that matter?

Both distributions install a package named `kafka`, so they write into the same directory and one overwrites files of the other. With `kafka==1.3.5` and `kafka-python==2.0.2` installed together on Python 3.12 the import fails with the same error. The agent reports the duplicate and proposes keeping only kafka-python.

### Will the agent test against a real Kafka broker?

Not unless you ask for it and the sandbox can reach one. By default it verifies that every client name the code uses imports on the target Python. Constructing a producer with no broker reachable ends in `NoBrokersAvailable`, which is as far as an offline check goes, and the pull request says that delivery was not exercised.

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

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

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