Install & Compatibility
Where this runs
tested against v4.9.0.20260518 · 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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.030s · 18.4MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.026s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
spawn
✓ import pexpect
child = pexpect.spawn('command')
types-pexpect is a stub-only package. You import symbols directly from the 'pexpect' library, and type checkers will automatically use the installed stubs for static analysis.
EOF
✓ import pexpect
child.expect(pexpect.EOF)
types-pexpect does not contain runtime code; it only provides type information for existing pexpect symbols.
This quickstart demonstrates a basic usage of pexpect with type checking. First, ensure both `pexpect` and `types-pexpect` are installed. The example spawns a simple Python script, captures its output, and decodes it. A type checker like MyPy can then verify the type correctness, leveraging the `types-pexpect` stubs. For instance, the `child.before` attribute's type (AnyStr | None) is correctly recognized.
import pexpect
import os
def run_and_check_type() -> str:
# Simulate a command that prints to stdout
script_path = 'temp_script.py'
with open(script_path, 'w') as f:
f.write("import sys; sys.stdout.write('hello world\n')")
try:
child = pexpect.spawn(f'python {script_path}')
child.expect('hello world\r\n') # pexpect typically sees \r\n
# In pexpect 4.9, 'before' is AnyStr | None
# You need to explicitly decode if you expect a str and were expecting bytes
# The types-pexpect 4.9.0 stubs reflect this (Issue #11382 on typeshed).
output_bytes = child.before
if output_bytes is None:
return ''
return output_bytes.decode('utf-8')
except pexpect.exceptions.TIMEOUT:
print('TIMEOUT exception')
return ''
except pexpect.exceptions.EOF:
print('EOF exception')
return ''
finally:
if os.path.exists(script_path):
os.remove(script_path)
if __name__ == '__main__':
result = run_and_check_type()
print(f"Captured output: {result}")
# To run type checking, save this as `your_script.py`
# Then run: `mypy your_script.py` after installing `mypy`
# Expected output: No issues found
Debug
Known issues
breakingType changes in pexpect.spawn().before from 'str' to 'AnyStr | None' in types-pexpect 4.9.0 stubs. If your code previously assumed `pexpect.spawn().before` would always be `str` and directly called `.decode()` without checking for `None` or handling `bytes`, type checkers will now flag this as an error.fixExplicitly check if `child.before` is `None` before attempting to decode it, and handle the possibility of it being `bytes` by calling `.decode()`.
affects: types-pexpect >=4.9.0.20240208 (corresponding to pexpect 4.9.*)
gotchaInstalling `types-pexpect` does not install the `pexpect` runtime library itself. You must install `pexpect` separately for your code to run, as `types-pexpect` only provides type information.fixAlways ensure both `pexpect` and `types-pexpect` are installed via `pip install pexpect types-pexpect`.
affects: All versions
gotchaRegular expression matching for end-of-line (`$` or `\n`) in `expect()` can be tricky due to how pseudo-TTYs handle line endings (typically `\r\n`). `$` often matches the end of the *current buffer*, not a logical line end, because Pexpect reads character by character.fixExplicitly expect `\r\n` for line endings. For general stream matching, avoid `$` for end-of-line and understand that regular expressions operate on the incrementally read buffer.
affects: All versions of pexpect (reflected in stubs)
gotchaA common 'timing issue with send() and sendline()' exists when interacting with applications that prompt for passwords. If `sendline()` is called immediately after `expect()` for a password prompt, the password might be echoed before the child application turns off echo.fixPexpect 4.0+ mitigates this with a default delay. If issues persist, consider adding a small `time.sleep()` before `sendline()`, or adjust the `delaybeforesend` attribute of the `spawn` object.
affects: All versions of pexpect (pexpect 4.0+ adds a default delay, but custom applications might still trigger it)
Upgrade
Version history
4.9.0.20260518latest on PyPI · released May 18, 2026
Audit
Dependencies
pexpectrequiredThis package provides type stubs for the 'pexpect' runtime library, which must be installed separately.
pythonrequiredRequires Python 3.10 or newer.