How to Check if a Python Package is Installed or Not?

In this article, we will learn how to determine whether or not a Python package is installed on your local machine running Python or Not

We need to know how to import them as well as how to determine whether or not they are installed.

Checking if a Python Package is Installed or Not

A Python package is a collection of modules and subpackages. A Python package must always include a __init__.py file. There are numerous methods for determining whether or not a Python package is installed.

Among them let us see 3 methods given below:

Method #1: Using the import keyword

Importing a package is one approach to see if it is installed or not. It will not display any errors if it is installed. If it is not installed, an import error message will be displayed. Another approach is to employ exception handling to get the same result.

Approach:

  • Using the try-except blocks to handle the errors
  • Inside the try- block import matplotlib module using the import keyword
  • Handle the ImportError using the except block.
  • Print the error if ImportError occurs.
  • The Exit of the Program.

Below is the implementation:

# Using the try-except blocks to handle the errors
try:
    # Inside the try- block import matplotlib module using the import keyword
    import matplotlib
# Handle the ImportError using the except block. 
except ImportError as error:
    # Print the error if ImportError occurs
    print(error)

Output:

We will get the below output if matplotlib module is not installed.

ModuleNotFoundError: No module named 'matplotlib'

If, on the other hand, matplotlib is installed, no error message will be displayed.

Method #2: Pip without having to import the package

Another approach to see if a package is installed is to use the pip freeze command in the terminal. This will display a list of all the installed packages.

To find a specific package, use the grep command in the Linux terminal, as illustrated below:

pip freeze | grep pandas

Output:

If the pandas module is present, the output is as follows:

pandas ==1.18.1.

The terminal will not produce any output if pandas is not installed. Instead of using grep on Windows, we may use findstr, which searches for a certain word in a given list in the same way that grep does on Linux.

Note: In windows, we use findstr instead of grep.

Method #3: Using the importlib.util module

Approach:

  • Import util function from importlib module using the import keyword.
  • Give the package/ module name as static input and store it in a variable.
  • Pass the above-given package to the find_spec() function to check if it is installed or not.
  • Check if the above result returns None or Not(If it returns None it means the package is not installed and vice-versa).
  • Else print “Yes, the package is installed”.
  • The Exit of the Program.

Below is the implementation:

# Import util function from importlib module using the import keyword
import importlib.util 
# Give the package/ module name as static input and store it in a variable
gvn_package= 'pywhatkit'
# Pass the above given package to the find_spec() function to check if it is installed or not
is_installed = importlib.util.find_spec(gvn_package)
# Check if the above result returns None or Not(If it returns None it means the package is not installed and viceversa)
if is_installed is None:
  print("Sorry, the "+ gvn_package+" package is not installed")
else:
  # Else print "Yes, the package is installed"
  print ("Yes, the package is installed")

Output:

Sorry, the pywhatkit package is not installed