Find all packages installed with easy_install/pip?

前端 未结 18 1087
醉话见心
醉话见心 2020-11-27 08:47

Is there a way to find all Python PyPI packages that were installed with easy_install or pip? I mean, excluding everything that was/is installed with the distributions tool

相关标签:
18条回答
  • 2020-11-27 09:45

    pip.get_installed_distributions() will give a list of installed packages

    import pip
    from os.path import join
    
    for package in pip.get_installed_distributions():
        print(package.location) # you can exclude packages that's in /usr/XXX
        print(join(package.location, package._get_metadata("top_level.txt"))) # root directory of this package
    
    0 讨论(0)
  • 2020-11-27 09:47

    pip freeze will output a list of installed packages and their versions. It also allows you to write those packages to a file that can later be used to set up a new environment.

    https://pip.pypa.io/en/stable/reference/pip_freeze/#pip-freeze

    0 讨论(0)
  • 2020-11-27 09:49

    pip list [options] You can see the complete reference here

    0 讨论(0)
  • 2020-11-27 09:50

    Newer versions of pip have the ability to do what the OP wants via pip list -l or pip freeze -l (--list).
    On Debian (at least) the man page doesn't make this clear, and I only discovered it - under the assumption that the feature must exist - with pip list --help.

    There are recent comments that suggest this feature is not obvious in either the documentation or the existing answers (although hinted at by some), so I thought I should post. I would have preferred to do so as a comment, but I don't have the reputation points.

    0 讨论(0)
  • 2020-11-27 09:50

    As @almenon pointed out, this no longer works and it is not the supported way to get package information in your code. The following raises an exception:

    import pip
    installed_packages = dict([(package.project_name, package.version) 
                               for package in pip.get_installed_distributions()])
    

    To accomplish this, you can import pkg_resources. Here's an example:

    import pkg_resources
    installed_packages = dict([(package.project_name, package.version)
                               for package in pkg_resources.working_set])
    

    I'm on v3.6.5

    0 讨论(0)
  • 2020-11-27 09:50

    If you use the Anaconda python distribution, you can use the conda list command to see what was installed by what method:

    user@pc:~ $ conda list
    # packages in environment at /anaconda3:
    #
    # Name                    Version                   Build  Channel
    _ipyw_jlab_nb_ext_conf    0.1.0            py36h2fc01ae_0
    alabaster                 0.7.10           py36h174008c_0
    amqp                      2.2.2                     <pip>
    anaconda                  5.1.0                    py36_2
    anaconda-client           1.6.9                    py36_0
    

    To grab the entries installed by pip (including possibly pip itself):

    user@pc:~ $ conda list | grep \<pip
    amqp                      2.2.2                     <pip>
    astroid                   1.6.2                     <pip>
    billiard                  3.5.0.3                   <pip>
    blinker                   1.4                       <pip>
    ez-setup                  0.9                       <pip>
    feedgenerator             1.9                       <pip>
    

    Of course you probably want to just select the first column, which you can do with (excluding pip if needed):

    user@pc:~ $ conda list | awk '$3 ~ /pip/ {if ($1 != "pip") print $1}'
    amqp        
    astroid
    billiard
    blinker
    ez-setup
    feedgenerator 
    

    Finally you can grab these values and pip uninstall all of them using the following:

    user@pc:~ $ conda list | awk '$3 ~ /pip/ {if ($1 != "pip") print $1}' | xargs pip uninstall -y
    

    Note the use of the -y flag for the pip uninstall to avoid having to give confirmation to delete.

    0 讨论(0)
提交回复
热议问题