How to Install pexpect in Python

v4.9.0 Testing & QA ISC license

Pexpect allows easy control of interactive console applications.

Install pip install pexpect

What is pexpect?

Pexpect allows easy control of interactive console applications.

Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands.

Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python.

The main features of Pexpect require the pty module in the Python standard library, which is only available on Unix-like systems. Some features—waiting for patterns from file descriptors or subprocesses—are also available on Windows.

Quick Start

Minimal example to get started with pexpect:

import pexpect

print(pexpect.__version__)

Installation

pip (standard)

pip install pexpect

Virtual environment (recommended)

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install pexpect

pip3

pip3 install pexpect

conda

conda install -c conda-forge pexpect

Poetry

poetry add pexpect

Dependencies

Installing pexpect will also install these packages:

Verify the Installation

After installing, confirm the package is available:

python -c "import pexpect; print(pexpect.__version__)"

If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.

Installation Errors

Common errors when installing pexpect with pip.

ModuleNotFoundError: No module named 'pexpect'

Cause: The package is not installed in the current Python environment.

Fix: Run pip install pexpect. If using a virtual environment, ensure it is activated first.

ModuleNotFoundError: No module named 'pexpect' (installed but still failing)

Cause: pip installed the package into a different Python than the one running your script.

Fix: Use python -m pip install pexpect to install into the interpreter you are running.

ImportError: cannot import name 'X' from 'pexpect'

Cause: The function or class does not exist in the installed version.

Fix: Check the version with pip show pexpect and upgrade with pip install --upgrade pexpect.

pip: command not found

Cause: pip is not in PATH or Python was not added to PATH during installation.

Fix: Try python -m pip install pexpect. On macOS/Linux try pip3.

PermissionError: [Errno 13] Permission denied

Cause: No write access to the system Python package directory.

Fix: Use a virtual environment, or add --user: pip install --user pexpect

SSL: CERTIFICATE_VERIFY_FAILED

Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.

Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pexpect

Recent Releases

VersionReleased
4.9.0 latest 2023-11-25
4.8.0 2020-01-21
4.7.0 2019-04-07
4.6.0 2018-05-29
4.5.0 2018-04-13

Full release history on PyPI →

Manage pexpect

Upgrade to latest version

pip install --upgrade pexpect

Install a specific version

pip install pexpect==4.9.0

Uninstall

pip uninstall pexpect

Check what is installed

pip show pexpect

Last updated: 2026-04-11 • Data from PyPI