Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Check If Python Package Is Installed?
In Python, we often need to check if a package is installed before using it in our programs. Python provides several built-in methods to verify package installation in your environment.
A package is a directory containing one or more Python modules with an __init__.py file. Packages allow developers to create reusable code that can be imported into other programs, avoiding code duplication.
Using try-except
The most straightforward approach is to use a try-except block. When Python attempts to import an uninstalled package, it raises an ImportError exception.
Example
Here's how to check if a package exists using try-except ?
try:
import Numpy as np
print("The package is installed")
except ImportError as error:
print(f"Import error: {error}")
Import error: No module named 'Numpy'
Since Python is case-sensitive, "Numpy" (with capital N) doesn't exist. Let's try with the correct name ?
try:
import numpy as np
print("The package is installed")
except ImportError as error:
print(f"Import error: {error}")
The package is installed
Using importlib.util
The importlib.util module provides the find_spec() function to check package availability without actually importing it.
Example
This method checks if a package specification exists ?
import importlib.util
package = "django"
if importlib.util.find_spec(package) is None:
print(f"{package} is not installed")
else:
print(f"{package} is installed")
django is not installed
Using pkgutil Module
The pkgutil module provides utilities for working with packages. Its find_loader() function can locate package loaders.
Example
Here's how to use pkgutil to check package installation ?
import pkgutil
package = "pandas"
if pkgutil.find_loader(package) is None:
print(f"{package} is not installed")
else:
print(f"{package} is installed")
pandas is installed
Using pkg_resources Module
The pkg_resources module's get_distribution() function checks for installed packages and provides detailed package information.
Example
This method provides more detailed package information ?
import pkg_resources
package = "numpy"
try:
distribution = pkg_resources.get_distribution(package)
print(f"{package} is installed (version: {distribution.version})")
except pkg_resources.DistributionNotFound:
print(f"{package} is not installed")
numpy is installed (version: 1.21.0)
Testing with a non-existent package ?
import pkg_resources
package = "nonexistent"
try:
distribution = pkg_resources.get_distribution(package)
print(f"{package} is installed (version: {distribution.version})")
except pkg_resources.DistributionNotFound:
print(f"{package} is not installed")
nonexistent is not installed
Comparison
| Method | Imports Package? | Provides Version Info? | Best For |
|---|---|---|---|
try-except |
Yes | No | Simple checks with import |
importlib.util |
No | No | Lightweight checking |
pkgutil |
No | No | Package utilities |
pkg_resources |
No | Yes | Detailed package info |
Conclusion
Use try-except for simple import-based checking, importlib.util.find_spec() for lightweight verification, or pkg_resources.get_distribution() when you need version information. Choose the method that best fits your specific requirements.
