python/cpython · error

cannot open '%s' for writing

Error message

cannot open '%s' for writing

What it means

Build-time error from Programs/_freeze_module.c: write_frozen() cannot fopen() the output C file for writing ('w' mode). The directory does not exist, permissions are wrong, or the path is invalid, so the marshalled frozen module cannot be emitted and the build step fails.

Source

Thrown at Programs/_freeze_module.c:195

        size_t i, end = Py_MIN(n + 16, data_size);
        fprintf(outfile, "    ");
        for (i = n; i < end; i++) {
            fprintf(outfile, "%u,", (unsigned int) data[i]);
        }
        fprintf(outfile, "\n");
    }
    fprintf(outfile, "};\n");
}

static int
write_frozen(const char *outpath, const char *inpath, const char *name,
             PyObject *marshalled)
{
    /* Open the file in text mode. The hg checkout should be using the eol extension,
       which in turn should cause the EOL style match the C library's text mode */
    FILE *outfile = fopen(outpath, "w");
    if (outfile == NULL) {
        fprintf(stderr, "cannot open '%s' for writing\n", outpath);
        return -1;
    }

    fprintf(outfile, "%s\n", header);
    char *arrayname = get_varname(name, "_Py_M__");
    if (arrayname == NULL) {
        fprintf(stderr, "memory error: could not allocate varname\n");
        fclose(outfile);
        return -1;
    }
    write_code(outfile, marshalled, arrayname);
    free(arrayname);

    if (ferror(outfile)) {
        fprintf(stderr, "error when writing to '%s'\n", outpath);
        fclose(outfile);
        return -1;
    }

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Create the output directory first: `mkdir -p $(dirname out.c)` then rerun
  2. Fix permissions on the target directory (chown/chmod) so the build user can write
  3. Use the Makefile targets instead of invoking _freeze_module by hand so paths are set up correctly
Defensive patterns

Strategy: validation

Validate before calling

# shell: create and permission the output dir before freezing
# mkdir -p "$(dirname "$OUT")" && [ -w "$(dirname "$OUT")" ] || exit 1

Prevention

When it happens

Trigger: Running `_freeze_module name in.py out.c` where out.c's parent directory does not exist or is read-only; builds where the output dir (e.g. build/ generated objects) was not created first; out-of-tree builds with bad relative paths.

Common situations: Manually invoking _freeze_module without mkdir of the target dir; a clean-ish tree missing the build output directory; permission issues when building as a different user than the checkout owner.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/46d5a963204be430. Report an issue: GitHub.