Registry / testing / pexpect

pexpect

JSON →
library4.9.0pypypi✓ verified 47d ago

Pure Python module for spawning child applications and controlling them — automates interactive CLI programs like ssh, ftp, passwd. Current version is 4.9.0 (Nov 2023). pexpect.spawn requires Unix/Linux (uses the pty module) — not available on Windows. For Windows use pexpect.PopenSpawn instead. Low maintenance activity since 2023.

testingdevops
pip install pexpect
Install & Compatibility
Where this runs
tested against v4.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.026s · 18.3MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.6s · import 0.024s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

spawn
from pexpect import spawn
import pexpect
EOF
from pexpect import EOF
TIMEOUT
from pexpect import TIMEOUT

Always pass encoding='utf-8'. expect() returns index. Use PopenSpawn on Windows.

import pexpect # Basic spawn with string encoding (recommended) child = pexpect.spawn( 'python3 -c "name = input(\"Name: \"); print(f\"Hello {name}!\")"', encoding='utf-8', timeout=10 ) # expect() waits for a pattern, returns the index of the match child.expect('Name: ') child.sendline('Alice') child.expect(pexpect.EOF) # wait for program to finish print(child.before) # 'Hello Alice!\r\n' # Multi-pattern expect index = child.expect(['pattern1', 'pattern2', pexpect.EOF, pexpect.TIMEOUT]) # index 0 = matched 'pattern1' # index 1 = matched 'pattern2' # index 2 = EOF (process ended) # index 3 = timeout # Windows: use PopenSpawn (no pty, no interactive echo) from pexpect import popen_spawn child = popen_spawn.PopenSpawn('python --version', encoding='utf-8') child.expect(pexpect.EOF) print(child.before)
Debug
Known issues
breakingpexpect.spawn is not available on Windows — it requires the pty module which only exists on Unix. Importing pexpect works, but calling pexpect.spawn() raises ImportError or OSError on Windows.
fix
On Windows use pexpect.popen_spawn.PopenSpawn instead. PopenSpawn lacks interactive TTY features but works cross-platform.
affects: all
breakingThe async= parameter was renamed to async_= as async became a Python keyword in 3.7. Using async= raises SyntaxError on Python 3.7+.
fix
Replace child.expect('pattern', async=True) with child.expect('pattern', async_=True)
affects: >= 4.3
gotchaWithout encoding='utf-8', child.before and child.after return bytes, not str. Most tutorials show str patterns but they fail with bytes. Always specify encoding at spawn time.
fix
Always pass encoding='utf-8': pexpect.spawn('cmd', encoding='utf-8'). Then expect() patterns and before/after are strings.
affects: all
gotchapexpect.TIMEOUT and pexpect.EOF are classes, not exceptions. They are passed to expect() as patterns (expect([pexpect.EOF, pexpect.TIMEOUT])), not caught with except. Catching them with except does not work as expected.
fix
Pass them as patterns in the expect list and check the returned index: index = child.expect(['pattern', pexpect.EOF, pexpect.TIMEOUT]); if index == 2: handle_timeout()
affects: all
gotchachild.before contains output BEFORE the matched pattern. child.after contains the matched pattern itself. Output AFTER the match is buffered internally. Accessing child.before after EOF gives all remaining output.
fix
To get all output: child.expect(pexpect.EOF); print(child.before) captures everything before EOF.
affects: all
gotchapexpect.screen and pexpect.ANSI modules are deprecated. Do not import from them.
fix
Use the pyte package for terminal emulation instead.
affects: all
gotchaThe child process terminated prematurely, likely due to an internal error (e.g., SyntaxError, unhandled exception, command not found, permissions error) or reaching its natural end of output, before the expected pattern could be found. Pexpect reports this as `pexpect.exceptions.EOF`.
fix
Inspect the contents of `child.before` for error messages or diagnostic output from the child process. Fix the underlying issue in the child's command or script. To handle expected terminations gracefully without raising an exception, add `pexpect.EOF` to your expect list (e.g., `child.expect(['your pattern', pexpect.EOF])`) and check the returned index.
affects: all
gotchapexpect.exceptions.EOF can be raised if the child process terminates unexpectedly before the expected pattern is found. This often indicates an error or crash within the child process itself (e.g., SyntaxError, unhandled exception, program exit).
fix
Examine the `child.before` buffer for error messages or tracebacks from the child process that explain its termination. Ensure the command or script executed by the child process is syntactically correct and robust, handling its own internal errors gracefully to prevent premature exit.
affects: all
Upgrade
Version history
4.9.0latest on PyPI
Audit
Dependencies
ptyprocess>=0.5requiredRequired on Unix for pty-based spawn. Installed automatically.
Agent activity
45 hits · last 30 days
node
6
seranking-bot
4
ahrefsbot
2
Amazon
1
bytedance
1
Resources