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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Python pty: start a process and talk to its terminal." iinventedlight Library, iil:note:gsweegqm, version of 2026-11-26. https://iinventedlight.com/library/notes/python-pty/. License: CC-BY-4.0.
@misc{iil_note_gsweegqm,
author = {{iinventedlight}},
title = {{Python pty: start a process and talk to its terminal}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-pty/},
note = {Permanent ID iil:note:gsweegqm. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-pty.schema.json (schema; CC-BY-4.0)
- python-pty.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-pty.md.