---
source: https://iinventedlight.com/library/notes/python-py-compile.md
title: "Python py_compile: compile a source file to byte-code"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, py_compile — Compile Python source files; CPython, py_compile documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-24
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-24
as_of: 2026-11-24
confidence: medium
untested: "Not reproduced (2026-11-24). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-py-compile"
tags: [python, py-compile, bytecode, pyc, compile]
client: Librarian
sources:
  - id: S1
    title: "py_compile — Compile Python source files"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/py_compile.html
    accessed: 2026-11-24
    license: "PSF License Version 2"
  - id: S2
    title: "py_compile documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/py_compile.rst
    accessed: 2026-11-24
    license: "PSF License Version 2"
---
# Python py_compile: compile a source file to byte-code
**Answer:** The py_compile module can generate a byte-code file from a source file.

## Why it exists

This function is not often needed.

It can be useful when installing modules for shared use.

That helps if some users may not have permission to write the byte-code cache files in the source directory.

Another function is used when the module source file is invoked as a script.

## Errors and output

PyCompileError is raised when an error occurs while attempting to compile the file, if doraise is true.

If doraise is false, the default, an error string is written to sys.stderr.

No exception is raised in that default case.

This function returns the path to the byte-compiled file.

## See also

- [python-compileall](https://iinventedlight.com/library/notes/python-compileall/)
- [python-importlib](https://iinventedlight.com/library/notes/python-importlib/)
- [python-runpy](https://iinventedlight.com/library/notes/python-runpy/)

## Claims

- id: C1
  claim: The module can generate a byte-code file from a source file.
  source: S1
  evidence: "generate a byte-code file from a source file"

- id: C2
  claim: That can help when installing modules for shared use.
  source: S1
  evidence: "useful when installing modules for shared use"

- id: C3
  claim: With doraise true, a compile error raises PyCompileError.
  source: S1
  evidence: "a PyCompileError is raised when an error is encountered"

- id: C4
  claim: A second function is used when the module is invoked as a script.
  source: S2
  evidence: "invoked as a script"

## Sources

- [S1] py_compile — Compile Python source files. Python Software Foundation. https://docs.python.org/3/library/py_compile.html . License: PSF License Version 2. Accessed 2026-11-24.

- [S2] py_compile documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/py_compile.rst . License: PSF License Version 2. Accessed 2026-11-24.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, py_compile — Compile Python source files; CPython, py_compile documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
