How to Check If Python Package Is Installed?


In Python we have a number of modules and packages which needs to be installed to work with. We have various ways to check if a python package is installed in python environment.

Package is a directory which contains one or more python modules and also has the __init__.py file which is an initialization file. For using the package, we have to import it by its name.

These can be inbuilt packages or allows developers to create the reusable code that can be imported into other programs by avoiding the duplicating of code.

Using try and except

When we try to import any package in Python without installing it an exception will be generated. We can catch an exception and display an appropriate message using the try-except blocks.

If we wrap the import statement within this block in case of exception, the statement(s) in the except block will be executed (if any).

Example

The following is an example of try and except. In here, if the package is imported without error then it shows the package is installed otherwise it will raise an exception

try:
    import Numpy as np
    print("The package is installed")
except ImportError as error:
    print(error)  

Output

No module named 'Numpy'

Example

In the previous example, we imported Numpy as modules are case sensitive, it raised an error. So now we are importing numpy with right spellings and correct case sensitive format.

try:
    import numpy as np
    print("The package is installed")
except ImportError as error:
    print(error)  

Output

Below output checks whether the python package is installed using the try and except method.

The package is installed

Using importlib.util

In python, we have a module namely importlib.util, which can be used to check if the given python package is installed or not.

Example

In this example, we will try to pass the name of a package to the find_spec() function of the importlib.util,module.

import importlib.util
package = "django"
if importlib.util.find_spec(package) is None:
    print(package,"is not installed in python environment")
else:
    print(package,"is installed")

Output

django is not installed in python environment

Using pkgutil module

The pkgutil is the module available in python which provides the utilities to work with the packages. It has a function namely find_loader() function which helps us to check if the given package is installed or not.

Example

Here in this example, we will pass the package name to the find_loader() function of the pkgutil module; then it will return whether the given package is installed or not.

import pkgutil
package = "pandas"
if pkgutil.find_loader(package) is None:
    print(package,"is not installed in python environment")
else:
    print(package,"is installed")

Output

pandas is installed

Using pkg_resources module

We have the module named pkg_resources in python which has a function called get_distribution(), used to check if the package is installed or not.

Example

In this example, we will pass the package name as the input argument to the get_distribution()) function of the pkg_resources module then it return the status.

import pkg_resources
package = "numpy"
try:
    pkg_resources.get_distribution(package)
    print(package,"is installed")
except pkg_resources.DistributionNotFound:
    print(package,"is not installed")

Output

Following is the output –

numpy is installed

Example

Let’s see another example to understand the working of the pkg_resources module.

import pkg_resources
package = "andas"
try:	
    pkg_resources.get_distribution(package)
    print(package,"is installed")
except pkg_resources.DistributionNotFound:
    print(package,"is not installed")

Output

The output is displayed as –

andas is not installed

Updated on: 09-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements