How to Check if a Python package is installed

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Check if a Python package is installed
  2. Installing the module if it isn't already installed
  3. Check if a Python package is installed using find_spec
  4. Installing the package if it isn't already installed
  5. Checking if the module isn't installed

# Check if a Python package is installed

To check if a Python package is installed:

  1. Import the package in a try block.
  2. Use an except block to handle the potential ModuleNotFoundError.
  3. If the try block runs successfully, the module is installed.
main.py
try: import requests print('The requests module is installed.') except ModuleNotFoundError: print('The requests module is NOT installed.')

check if python package is installed

Make sure to replace requests with the name of your specific module.

We used a try/except block to check if a module is installed.

If the try block doesn't raise an exception, the module is installed.

If the module isn't installed, a ModuleNotFoundError error is raised and the except block runs.

# Installing the module if it isn't already installed

You can also extend the try/except statement to install the module if it isn't installed.

main.py
import sys import subprocess try: import requests print('The requests module is installed') except ModuleNotFoundError: print('The requests module is NOT installed') # ๐Ÿ‘‡๏ธ Optionally install module python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL ) finally: import requests

installing the module if it is not already installed

If you need to check if a package is installed using pip, use the pip show command.

shell
pip show requests

check if module installed using pip

The pip show module_name command will either state that the package is not installed or will show a bunch of information about the package, including the location where the package is installed.

You can also use the following one-liner command.

shell
python -c 'import pkgutil; print(1 if pkgutil.find_loader("module_name") else 0)'

Make sure to replace module_name with the actual name of the module you are checking for.

The command returns 1 if the module is installed and 0 if it isn't but this can be easily adjusted.

# Check if a Python package is installed using find_spec

An alternative approach is to use the importlib.util.find_spec method.

The find_spec() method will return a spec object if the module is installed, otherwise, None is returned.

main.py
import importlib.util module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec: print(f'The {module_name} module is installed') else: print(f'The {module_name} module is NOT installed')

check if python package is installed using find spec

The importlib.util.find_spec method finds the spec for a module.

The spec for a module is a namespace containing the import-related information used to load the module.

If no spec is found, the method returns None.

# Installing the package if it isn't already installed

You can optionally install the package if it isn't already installed.

main.py
import importlib.util import sys import subprocess module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec: print(f'The {module_name} module is installed') else: print(f'The {module_name} module is NOT installed') # ๐Ÿ‘‡๏ธ Optionally install the module if it's not installed python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL )

# Checking if the module isn't installed

If you need to check if the module isn't installed, check if the spec variable stores a None value.

main.py
import importlib.util import sys import subprocess module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec is None: print(f'The {module_name} module is NOT installed') # ๐Ÿ‘‡๏ธ Optionally install the module if it's not installed python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL ) print(f'The {module_name} module is now installed')

Which approach you pick is a matter of personal preference. I'd use the try/except statement because I find it quite direct and easy to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev