Get a list of packages installed in your current environment with python

Get a list of installed packages

You may want a list of packages installed in your current environment with python. A summary of what to do in such a case.

Command line

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

programming

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
"""

If you don't want to depend on pip

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

Get a list of packages installed in your current environment with python
Get a list of files in a folder with python without a path
How to get a list of files in the same directory with python
Get the number of specific elements in a python list
Get a list of purchased DMM eBooks with Python + Selenium
How to get a list of built-in exceptions in python
Get the value of a specific key in a list from the dictionary type in the list with Python
I quarantined my environment with virtualenv, but I get a lot of packages with pip list
Display a list of alphabets in Python 3
[python] Get a list of instance variables
[Python] Get a list of folders only
Try to get a list of breaking news threads in Python.
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Generate a list packed with the number of days in the current month.
Create a virtual environment with conda in Python
Make a copy of the list in Python
Get a list of IAM users with Boto3
Receive a list of the results of parallel processing in Python with starmap
Work in a virtual environment with Python virtualenv.
Get a glimpse of machine learning in Python
Get a quick Python development environment with Poetry
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Get a list of articles posted by users with Python 3 Qiita API v2
Get your current location with a USB GPS receiver
Recommendation of building a portable Python environment with conda
[python] Get the list of classes defined in the module
Get your current location and user agent in Python
Python: Get a list of methods for an object
Group by consecutive elements of a list in Python
How to identify the element with the smallest number of characters in a Python list?
Try running python in a Django environment created with pipenv
Update packages already installed with pip in user environment (non-su)
Developed a library to get Kindle collection list in Python
Since Python 1.5 of Discord, I can't get a list of members
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
Get a clean Python development environment with pyenv + pipx + Poetry
Get index of nth largest / smallest value in list in Python
Get started with Python! ~ ① Environment construction ~
Get the value of a specific key up to the specified index in the dictionary list in Python
Create a virtual environment with Python!
Recursively get the Excel list in a specific folder with python and write it to Excel.
Building a virtual environment with Python 3
List of packages installed by conda
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Get started with Python in Blender
Build a Python environment on your Mac with Anaconda and PyCharm
I tried to create a list of prime numbers with python
[python] Get the rank of the values in List in ascending / descending order
Get the number of readers of a treatise on Mendeley in Python
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Building a Python3 environment with Amazon Linux2
Create a python environment on your Mac
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
Get a local DynamoDB environment with Docker
How to get a stacktrace in python
Get a token for conoha in python