How to Install ‘Python-Pexpect’ Module in Linux

Pexpect is a Python module that allows you to spawn child applications, manage them as well as acknowledge expected patterns in their output. In addition, it is used for automating interactive applications such as ftp, ssh, passwd just to mention a few. You can also use it to automate Linux commands, setup scripts, and Software testing.

In this guide, we will demonstrate the installation of the Python-Pexpect python module in Linux.

Installing Python3 and Python3-PiP in Linux

The installation of Pexpect Python modules requires the following as prerequisites:

  • Python3
  • Python3-pip

Most of the today’s modern Linux distributions comes with Python3 by default and you can confirm this by running the command:

$ python3  -V
Check Python Version in Linux
Check Python Version in Linux

From the output, you can see that we are running Python 3.8.2.

To install pip3 in Linux, run the following command:

$ sudo apt install python3-pip      [On Debian, Ubuntu and Mint]
$ sudo yum install python3-pip      [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge --ask dev-python/pip  [On Gentoo Linux]
$ sudo pacman -S python-pip         [On Arch Linux]
$ sudo zypper install python3-pip   [On OpenSUSE]    

Once the installation is complete, confirm that pip3 has been installed:

$ pip3 --version
Check Python PIP Version in Linux
Check Python PIP Version in Linux

Install Python-Pexpect in Linux

With the prerequisites in place, go ahead and install the python-pexpect Python module using pip3 as shown.

$ pip3 install pyexpect
Install Python-Pexpect in Linux
Install Python-Pexpect in Linux

Once complete, run the command below to confirm the installation of the Python module:

$ python3 -m pip show pyexpect
Confirm Python-Pexpect in Linux
Confirm Python-Pexpect in Linux

How to Use Python-Pexpect in Linux

One of the ways of automating Linux command with the python-pexpect module is using the run() method in a Python program. The method can be called to run a Linux command and then return its value.

Consider the following checkUptime.py file.

The run() method takes the uptime -p Linux command as the argument and stores it in the checkUptime variable. The strip() method is then used to remove leasing and trailing characters from the output.

# Importing pexpect module  
import pexpect as px  

# Invoke run method with Linux command 
checkUptime = px.run('uptime -p') 
new_String = checkUptime.strip()

# Print result to stdout
print("The system has been up for: ", new_String.decode())

To run the Python code, execute:

$ python3 checkUptime.py

When executed, the code returns the duration the System has been running since it was powered on. This result is just the same as running the uptime -p command on the Linux shell.

This is a confirmation that the python-pexpect module can take and automate Linux commands.

Use Python-Pexpect in Linux
Use Python-Pexpect in Linux

And that’s it as far as the installation of Python-pexpect on Linux is concerned. Your feedback will be highly appreciated.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.