You may want a list of packages installed in your current environment with python. A summary of what to do in such a case.
If you just want to know the information, you can do the following on the console.
pip freeze
# .. output messages of command (example)
Jinja2==2.7.2
MarkupSafe==0.23
Pygments==1.6
Sphinx==1.2.2
docutils==0.11
If you want to output as List, for example, on some code. You can find out by reading the pip code. (Reference is pip / commands / freeze.py)
from pip.util import get_installed_distributions
skips = ['setuptools', 'pip', 'distribute', 'python', 'wsgiref']
for dist in get_installed_distributions(local_only=True, skip=skips):
print(dist.project_name, dist.version)
""" output of script
docutils 0.11
Jinja2 2.7.2
MarkupSafe 0.23
Pygments 1.6
Sphinx 1.2.2
"""
Take a look inside pip / util.py.
def get_installed_distributions(local_only=True,
skip=stdlib_pkgs,
include_editables=True,
editables_only=False):
"""
Return a list of installed Distribution objects.
If ``local_only`` is True (default), only return installations
local to the current virtualenv, if in a virtualenv.
``skip`` argument is an iterable of lower-case project names to
ignore; defaults to stdlib_pkgs
If ``editables`` is False, don't report editables.
If ``editables_only`` is True , only report editables.
"""
if local_only:
local_test = dist_is_local
else:
local_test = lambda d: True
if include_editables:
editable_test = lambda d: True
else:
editable_test = lambda d: not dist_is_editable(d)
if editables_only:
editables_only_test = lambda d: dist_is_editable(d)
else:
editables_only_test = lambda d: True
return [d for d in pkg_resources.working_set
if local_test(d)
and d.key not in skip
and editable_test(d)
and editables_only_test(d)
]
After all, I'm just narrowing down the objects in pkg_resources.working_set by conditions. So, if you don't need to narrow down, you can look at pkg_resources.working_set.
import pkg_resources
for dist in pkg_resources.working_set:
print(dist.project_name, dist.version)
# output of script
"""
docutils 0.11
Jinja2 2.7.2
MarkupSafe 0.23
pip 1.4.1
Pygments 1.6
setuptools 0.9.8
Sphinx 1.2.2
"""
Recommended Posts