Last updated: Apr 10, 2024
Reading timeยท3 min
To check if a Python package is installed:
try
block.except
block to handle the potential ModuleNotFoundError
.try
block runs successfully, the module is installed.try: import requests print('The requests module is installed.') except ModuleNotFoundError: print('The requests module is NOT installed.')
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.
You can also extend the try/except
statement to install the module if it isn't
installed.
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
If you need to check if a package is installed using pip
, use the pip show
command.
pip show requests
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.
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.
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.
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')
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.
You can optionally install the package if it isn't already installed.
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 )
If you need to check if the module isn't installed, check if the spec
variable
stores a None
value.
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.
You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir
option