1. Overview

Python is an open-source, interpreted programming language. It’s popular because of its ease of use, large community base, and many modules. In addition to its built-in modules, it’s possible to extend the functionality by using external modules. However external modules must be installed separately.

In this tutorial, we’ll discuss how to check whether a Python module is installed and how to install it if it isn’t installed.

2. Python Modules

Python modules look like the header files in the C programming language. A module is a file that contains several Python definitions and statements.

There are several built-in Python modules that come with the installation of Python. For example, the math module is a built-in module that contains mathematical functions.

We must import a module before using it. We use the import statement in a Python script to import a module:

import math

3. Checking the Installation of a Python Module

In this section, we’ll learn two methods for checking the installation of a Python module.

3.1. Using python

We can execute Python statements from the command line without writing Python scripts using the -c option of the python command:

$ python -c "import math"
$ echo $?
0

This command executes a single Python statement, import math, from the command line. We don’t get any errors since we import the built-in math module. Additionally, echo $? returns 0. Therefore, the exit code of the python -c “import math” command is 0, which is an indication of a successful command execution.

However, we get an error when we try to import a module that isn’t installed:

$ python -c "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
$ echo $?
1

The numpy module is a popular scientific calculation module. As is apparent from the output, this module isn’t installed. Additionally, the exit code of python -c “import numpy” is 1, which is an indication of a problem.

Therefore, we can check the installation of a Python module from the command line just by importing it using python -c.

3.2. Using pip

Another way of checking the installation of a module is using the list option of the pip command. pip is the package installer for Python.

Let’s check the installation of numpy using pip:

$ pip list --format=legacy | grep numpy
$ echo $?
1

The –format=legacy selects the output format. As there might be many Python modules installed, we filter the output using grep numpy.

There is no numpy module installed, as expected. The exit code is 1.

4. Installing Python Modules

We’ll learn two methods for installing a Python module in this section.

4.1. Using Package Management Tools

One method is to find the corresponding package and install it using the relevant package management tool. For example, we can use dnf or yum to search and install a Python module in RHEL:

$ sudo dnf list available | grep python3 | grep numpy
python3-numpy.x86_64                               1:1.14.3-10.el8                               @appstream

The package name is python3-numpy.x86_64 according to the output. The dnf list available command lists the available packages. We use the grep command to filter the output of dnf list available. Additionally, we use the sudo command since dnf requires root privileges.

We can install the package using the dnf install command together with sudo:

$ sudo dnf install python3-numpy.x86_64
…
Transaction Summary
================================================================================
Install  5 Packages
…
…
…
Installed:
  libgfortran-8.5.0-21.el8.x86_64        libquadmath-8.5.0-21.el8.x86_64       
  openblas-0.3.15-4.el8.x86_64           openblas-threads-0.3.15-4.el8.x86_64  
  python3-numpy-1:1.14.3-10.el8.x86_64  

Complete!

dnf install also installs the packages that python3-numpy.x86_64 depends on. In our case, there are five dependent packages.

Having installed python3-numpy.x86_64, let’s try it again:

$ python -c "import numpy"
$ echo $?
0

We’re successful in the installation of the numpy module.

4.2. Using pip

Another option for installing modules is to use the install option of the pip command. We need to pass the name of the package we want to install to pip:

$ pip install --user numpy
Collecting numpy
  Using cached https://files.pythonhosted.org/packages/45/b2/6c7545bb7a38754d63048c7696804a0d947328125d81bf12beaa692c3ae3/numpy-1.19.5-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.19.5

We use the –user option of pip. Otherwise, the installation requires root privileges. When installing a Python package with root privileges using pip, pip gives a warning and recommends using the –user option.

Now, let’s check the installation of numpy:

$ python -c "import numpy"
$ echo $?
0

Again, we’re successful in the installation of the numpy module.

5. Conclusion

In this article, we discussed how to check whether a Python module is installed and how to install it if it isn’t installed.

First, we discussed briefly what Python modules are. Then, we saw that it’s possible to check the installation of a module from the command line by trying to import it using python -c. We can also use the list option of pip to check the installation.

Finally, we learned how to install Python modules. Installation of a Python module using the package management tool of the Linux distro, for example, dnf or yum in RHEL, is one option. The other option is to use the install option of pip.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.