List all installed package names in Python

pkgs = lambda : list(__import__('pkg_resources').working_set)

pkg_names = lambda : [x.project_name for x in pkgs()]

pkg_versions = lambda : [x.project_name + '==' + x.version for x in pkgs()]
>>> pkg_names()
['ansible', 'pycrypto', 'PyYAML', 'Jinja2', '...truncated...', 'virt-back', 'Werkzeug', 'xmltodict']
>>> pkg_versions()
['ansible==1.7', 'pycrypto==2.6.1', '...truncated...', 'virt-back==0.1.0', 'xmltodict==0.9.2']

Leave a Reply

Your email address will not be published. Required fields are marked *