# Fix "ModuleNotFoundError: No module named 'telnetlib'" on Python 3.13

Yes. An agent finds what still imports the removed telnetlib module, picks a backport or a socket rewrite, and proves the exchange against a local telnet server.

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

## What the agent does

Python 3.13 removed `telnetlib`, so a file that says `import telnetlib` stops on that line. The agent first finds where the import lives and what reaches it, because that decides when the error shows. In google/mobly it is one file: `mobly/controllers/attenuator_lib/telnet_scpi_client.py` imports the module on line 21, and its `TelnetScpiClient` class is the transport for the Mini-Circuits attenuator driver in `minicircuits.py`. Nothing imports that driver at start-up. `mobly/controllers/attenuator.py` loads it by name with `importlib.import_module` when a test config has an `Attenuator` entry whose `model` is `minicircuits`. So `import mobly` works, `from mobly.controllers import attenuator` works, and the traceback arrives only when a test bed with an attenuator starts.

That placement is why the import can outlive the interpreter it needs. The project's `pyproject.toml` says `requires-python = ">=3.14"` and `.github/workflows/ci.yml` runs `tox` on Python 3.14 on three operating systems, yet `tests/mobly/controllers` holds only the Android device tests, so no test imports the attenuator driver and the workflow has nothing to fail on. The agent creates a 3.14 environment in the sandbox, imports `mobly.controllers.attenuator_lib.minicircuits`, and keeps the traceback as the starting evidence. On 3.12 the same import still works and prints `DeprecationWarning: 'telnetlib' is deprecated and slated for removal in Python 3.13`, which the agent records as the earliest sign.

Then it chooses a fix, and it tests the choice instead of assuming it. The client uses four calls: `open(host, port, 10)`, `read_until`, `write` and `expect` with one regular expression. One option is the `standard-telnetlib` package, which restores the module under its old name with no code change, at the cost of a new dependency in a project whose only ones are `portpicker`, `pyyaml` and, on Windows, `pywin32`. The other is a rewrite on `socket`, short because only those four calls are in use. To compare them the agent writes a small TCP server in the sandbox that answers `MN?` the way the driver expects and runs the driver against it twice: once plain, and once with the server opening the connection with telnet option bytes. `telnetlib` strips those bytes before the caller sees them. A plain socket does not, the reply then starts with byte `0xff`, and the driver's `decode()` raises `UnicodeDecodeError`. A rewrite therefore has to skip those sequences itself, and the agent says which option it takes and what the second exchange shows.

Last, it adds the test the suite was missing. The pytest settings in `pyproject.toml` collect files named `*_test.py` with classes named `*Test`, so the local server and both exchanges go in a new file under `tests/mobly/controllers` in that shape, and the workflow imports the driver from then on. It also lists what it noticed and left alone. Here that is a line in `cmd()` that calls `cmd_str.strip(self.tx_cmd_separator)` and discards the result, and a comment in `attenuator.py` that names the telnet library as the only transport.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Part of this project fails on Python 3.13 and later with \"ModuleNotFoundError: No module named 'telnetlib'\". Find every import of telnetlib, in the package, scripts, examples and tests, and for each one say what imports that file and when: at start-up, or only when a certain config or command is used. Create an environment on the newest Python version the project supports, reproduce the import error and save the traceback. List the telnetlib calls the code makes (open, read_until, write, expect, close and any others) and the timeouts it passes. Then compare two fixes and pick one: adding a backport package that provides telnetlib again, or replacing the calls with the socket module. Write a small local TCP server that answers the commands the code sends, and run the real client code against it twice, once plain and once with the server sending telnet option negotiation bytes (IAC sequences) before its first reply. A fix that fails either exchange is not finished. Keep the public classes, method names and timeouts unchanged. Add the local-server test to the project's test suite in the project's own naming style so CI imports this module from now on. Do not contact any real device. Open a pull request with the original traceback, the output of both exchanges before and after, the full test suite result, and a list of 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 one of two shapes. With the backport, the dependency list in `pyproject.toml` gains `standard-telnetlib` and no line of the client changes. With the rewrite, `telnet_scpi_client.py` loses `import telnetlib` and gains a socket connection, a read loop with the same one-second reply deadline, and the code that drops option sequences. Both shapes carry the new test file. The description holds the traceback from the newest supported interpreter, the two exchanges against the local server, and the summary line of the whole suite.

A reviewer checks three things. First, that the timeouts are the ones the old code passed: ten seconds to connect, two for the prompt, one for the reply. Second, that the negotiation exchange is in the test and not only in the description, since that is the case a quick rewrite gets wrong. Third, the limit the agent states plainly: the server in the sandbox is a stand-in written from the driver's own expectations, and no attenuator is contacted, so behaviour against real hardware is still unproven until someone runs one test bed.

## Run it on every import error from a test bed

This error surfaces on a lab machine after its Python moves, far from the repository. Have the runner report it: wrap the test command in a script that, when the output contains `No module named 'telnetlib'`, posts the traceback to the URL of a `webhook` trigger on an automation that holds the prompt above. The script sends `Authorization: Bearer` with the trigger's secret. The posted body, up to 64 KB, is placed after the saved prompt as event text, so the agent sees which config and which driver failed.

Several benches fail at once when a shared image changes, and one run fixes them all. Send an `Idempotency-Key` header to the trigger's URL built from the commit the benches run, so repeated posts start nothing new, and set the automation's max runs per hour to one or two.

## Why run it on Rush?

The fix needs two things a laptop rarely has ready: the newest interpreter in a clean environment, and a device to talk to. The sandbox supplies the first and the agent builds a stand-in for the second, a server it can make misbehave on purpose. Every command it runs, including the exchange that breaks the naive rewrite, is kept with the session, so the reviewer reads the evidence instead of trusting a summary.

It is also work that waits a long time for an owner. Code that only runs when hardware is attached sits outside every green check, and the person who finally hits the error is running a test bed, not maintaining the transport. A run that starts from that traceback and ends with a tested pull request moves the problem to the people who can merge it.

## Frequently asked questions

### What replaces telnetlib in Python 3.13?

Nothing in the standard library. The choices are a backport such as `standard-telnetlib`, which keeps `import telnetlib` working, an asyncio library such as `telnetlib3` with a different API, or the `socket` module when the code uses only a few calls. The agent picks by testing the code's real exchanges against each.

### Can I swap telnetlib for a plain socket?

Only if you handle what `telnetlib` handled for you. It removes telnet option sequences from the data and refuses the options a server offers. A socket client that ignores them works against a quiet server and fails when a device opens with negotiation, which is the case the agent's second exchange covers.

### Why did CI not catch this?

Because nothing in the suite imported the file. A module that is loaded by name at run time, for hardware most contributors do not have, is invisible to a test run. The pull request adds a test that imports and exercises it, so the next removal fails a check instead of a lab run.

### Does the error affect Python 3.12?

No. On 3.12 the import succeeds and Python prints a DeprecationWarning that names 3.13 as the removal. Projects that run tests with warnings shown had notice; the agent quotes that warning in the pull request for the same reason.

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

- [Fix "Node.js 20 actions are deprecated" Annotations in GitHub Actions Workflows](https://getrush.ai/run/fix-node-js-20-actions-are-deprecated/github-actions/)
- [Fix "Node Sass does not yet support your current environment" on a current Node release](https://getrush.ai/run/fix-node-sass-unsupported-environment/node-sass/)
- [Fix "ValueError: 'not' is not a valid parameter name" in a FastAPI App on Python 3.11 or Later](https://getrush.ai/run/fix-not-is-not-a-valid-parameter-name/pydantic/)
- [Fix "np.alltrue was removed in the NumPy 2.0 release"](https://getrush.ai/run/fix-np-alltrue-was-removed-in-the-numpy-2-release/numpy/)
- [Fix "ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject"](https://getrush.ai/run/fix-numpy-dtype-size-changed/numpy/)
- [Fix "AttributeError: module 'numpy' has no attribute 'float'" on NumPy 1.24 and Later](https://getrush.ai/run/fix-numpy-float-attribute-error/numpy/)

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