---
source: https://iinventedlight.com/library/notes/python-pty.md
title: "Python pty: start a process and talk to its terminal"
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, pty — Pseudo-terminal utilities; CPython, pty 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-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). 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-pty"
tags: [python, pty, pseudo-terminal, fork, unix]
client: Librarian
sources:
  - id: S1
    title: "pty — Pseudo-terminal utilities"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/pty.html
    accessed: 2026-11-26
    license: "PSF License Version 2"
  - id: S2
    title: "pty documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/pty.rst
    accessed: 2026-11-26
    license: "PSF License Version 2"
---
# Python pty: start a process and talk to its terminal
**Answer:** The pty module defines operations for handling a pseudo-terminal, including starting another process and reading from and writing to its controlling terminal.

## Platform and fork

Availability is Unix only.

Pseudo-terminal handling is highly platform dependent.

Linux, FreeBSD, and macOS are the main tested systems. It is supposed to work on other POSIX platforms but it has not been thoroughly tested.

pty.fork starts a child and attaches that child's controlling terminal to a pseudo-terminal.

The return value is a pair (pid, fd). The child gets pid 0. The parent receives the child's pid, plus a file descriptor connected to that controlling terminal.

## openpty and spawn

openpty creates a new master and slave pair. It prefers os.openpty, and otherwise uses emulation for generic Unix systems.

The result is two file descriptors, one for each end of the pair.

spawn starts a process and hooks its controlling terminal to the current process's standard I/O.

## See also

- [python-os](https://iinventedlight.com/library/notes/python-os/)
- [python-termios](https://iinventedlight.com/library/notes/python-termios/)
- [python-fcntl](https://iinventedlight.com/library/notes/python-fcntl/)

## Claims

- id: C1
  claim: The pty module handles the pseudo-terminal concept.
  source: S1
  evidence: "The pty module defines operations for handling the pseudo-terminal concept:"

- id: C2
  claim: Availability is Unix only.
  source: S1
  evidence: "Availability : Unix."

- id: C3
  claim: openpty returns a master and slave file descriptor pair.
  source: S2
  evidence: "Return a pair of file descriptors"

## Sources

- [S1] pty — Pseudo-terminal utilities. Python Software Foundation. https://docs.python.org/3/library/pty.html . License: PSF License Version 2. Accessed 2026-11-26.

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

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, pty — Pseudo-terminal utilities; CPython, pty 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).
