All jobs

Fix "AttributeError: module 'os' has no attribute 'errno'" on Python 3.7 and Later

Yes. An agent replaces os.errno, and first finds the failure it was hiding: this error only appears while another exception is already being handled.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

os.errno was never a documented name. The os module happened to import errno for its own use, so os.errno.EEXIST worked by accident until Python 3.7, when that internal import went away. Adding import errno and writing errno.EEXIST makes the message disappear. Before doing that, the agent looks at where the name is used, because it is almost always inside an except OSError block. That placement means the AttributeError is the second exception, raised while handling a first one, and the first one is the real report.

neulab/xnmt has a clear case in recipes/las-tedlium/extract_db.py. Its make_parent_dir checks os.path.exists(os.path.dirname(filename)), calls os.makedirs on that directory if the check fails, and in except OSError as exc compares exc.errno != os.errno.EEXIST before re-raising. On a normal run the handler is never entered, so the script works and nobody learns the line is broken. The handler runs in three situations: another process creates the directory between the check and the call, the parent is not writable, or the filename has no directory part. In the last case os.path.dirname returns an empty string, os.path.exists('') is false and os.makedirs('') raises FileNotFoundError. In all three, Python 3.7 and later report module 'os' has no attribute 'errno' on top of the original error.

The agent reads the traceback from the bottom up, finds the line During handling of the above exception, another exception occurred, and names the first exception in its report. It then searches the repository for other copies of the helper. xnmt has one in xnmt/utils.py that already imports errno and also guards the empty case with os.path.dirname(filename) or ".", so the script's copy is an older duplicate of a function the package has since fixed. The agent says so, and proposes that the script import the package's helper or copy its current form.

For the edit itself it prefers the form that removes the handler: os.makedirs(path, exist_ok=True) behind a check for an empty directory name. That deletes the check-then-create race and the errno comparison together. It keeps the behaviour for a read-only parent, where PermissionError should still reach the caller, and adds tests for all three situations using a temporary directory: an existing directory, a bare filename, and a parent with its write bit removed. recipes/las-tedlium/prep-data.sh creates the db and transcript directories with mkdir before it calls the script, which is why the documented flow never reached the broken line. The agent notes that as well.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This project raises \"AttributeError: module 'os' has no attribute 'errno'\" on Python 3.7 or later. Search the whole repository, including scripts, examples and tests, for os.errno and list every use with its file, line and the enclosing function. For each use, say whether it sits inside an except block. If it does, identify which call in the try block can raise, list the concrete conditions under which it raises (directory already exists, parent not writable, empty path, path is a file, race with another process), and state which original exception a user would have seen before Python 3.7. Reproduce each condition in a temporary directory and show the full traceback, including the first exception. Then fix it: where the code is a create-directory-if-missing helper, replace the try and except with os.makedirs(path, exist_ok=True) and guard the case where os.path.dirname returns an empty string; elsewhere import errno and use it directly. Do not swallow errors that used to propagate, such as permission errors. If the repository contains more than one copy of the same helper, say which copies differ and propose keeping one. Add tests for an existing directory, a bare filename and a read-only parent. Run the test suite and include the commands and output. Open a pull request that describes the original exception each change uncovers.",
  "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 whose description starts with the traceback as users see it and then the same traceback annotated: which exception came first, on which line, and why the handler failed to deal with it. For each use of os.errno there is a short entry with the conditions that reach it and the exception that was hidden. The diff is a few lines per helper, plus a test file.

A reviewer checks two things. First, that errors which should still stop the program do: the read-only test must expect PermissionError, and a path that exists as a regular file must still fail, since exist_ok=True only forgives an existing directory. Second, that the bare-filename case now does what the caller wants, which is usually to write into the current directory. If the repository had duplicate helpers, the description lists them and says which one the pull request kept, so the reviewer can decide whether a standalone script may import from the package.

Run it on every new Sentry issue

This error tends to arrive from production, in a job that hit a full or read-only disk and then reported the wrong thing. Sentry signs its own webhook requests and cannot send the Authorization: Bearer header a Rush webhook trigger needs, so a small handler on your server receives the Sentry webhook, checks that the issue title contains has no attribute 'errno', and sends this request with the event's traceback appended to input.

Have the handler send a request_id that is a UUID v5 of the Sentry issue id, so a redelivered alert returns the run it already started. The API key's webhook_url then receives run.completed with the answer and the session URL, which the handler can post back to the issue as a comment.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Each condition that reaches the broken line needs a filesystem arranged for it: a directory that already exists, a parent with no write permission, a path with no directory part. Setting those up on a laptop means making and cleaning scratch directories and remembering that a root shell ignores permission bits. The agent builds each case in its own sandbox as an ordinary user, runs the helper, and the tracebacks it gets are stored with the session, so the pull request quotes real output for each case.

The alert that starts this job also names the wrong problem. Someone triaging by title files it as a Python upgrade chore. A run that reads the chained traceback first comes back with the underlying failure, which may be a deployment whose data directory is not writable, and that is worth knowing before the merge.

Frequently asked questions

Why did os.errno work before Python 3.7?

The os module used to import errno internally, and any module-level import is reachable as an attribute. It was never part of the documented interface. When the internal import was removed in 3.7, the accidental attribute went with it.

Is adding import errno enough?

It removes the AttributeError, and the handler then does what it was written to do. It does not tell you why the handler ran. If your logs show this error, some call inside the try block failed first, and the agent reports which one and under what conditions.

Can I just use os.makedirs(path, exist_ok=True)?

For a create-if-missing helper, yes, and it is the shorter and safer form because it has no gap between checking and creating. Two details remain: os.makedirs('', exist_ok=True) still raises FileNotFoundError, so a bare filename needs a guard, and a path that exists as a file still raises.

Why do my tests pass if the code is broken?

The broken name is only evaluated when the try block fails. Tests that create directories in a fresh temporary folder never take that branch. The agent adds tests that do, which is the only way to keep the handler from breaking silently again.

More jobs like this: Fix a bug from an alert

Keep reading