import pkg_resources
packages = [dist.project_name for dist in pkg_resources.working_set]
try:
for count, item in enumerate(packages):
print(item, pkg_resources.get_distribution(item).version)
except:
pass here
The indentations might not be perfect. The reason I am using a Try- Except block is that few library names will throw errors because of parsing the library names to process the versions. even though packages variable will contain all the libraries install in your environment.