How do I check the versions of Python modules?
You can use the built-in "pkg_resources" module to check the version of a Python module.
You can use the built-in importlib.metadata module to check the version of a Python module. Here is a code snippet that shows how to check the version of the "pandas" module:
Check the versions of Python modules inside a Python script
import importlib.metadata
module_name = "pandas"
try:
module_version = importlib.metadata.version(module_name)
print(f"{module_name} version: {module_version}")
except importlib.metadata.PackageNotFoundError:
print(f"{module_name} is not installed.")This code snippet imports the importlib.metadata module, assigns the name of the module you want to check (in this case, "pandas") to a variable, and uses the version() function to retrieve the installed version. It also includes error handling to gracefully manage cases where the module is not installed.
You can also use the pip package manager to check a module's version from the command line.
Check the versions of Python modules using pip
pip show pandasThis will display the version of the pandas module installed on your machine.