← All notes

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

Sources

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

Permanent ID: iil:note:gsweegqm

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.},
}

Markdown

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.

All artifact files (JSON index)

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.