ChatGPT解决这个技术问题 Extra ChatGPT

Find all packages installed with easy_install/pip?

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 tools (in this case apt-get on Debian).


T
TheodorosPloumis

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


As of Version 1.3 'pip list' has been added. See my answer below and the docs here: pip-installer.org/en/latest/usage.html#pip-list
list and freeze output different formats (as of the time of writing). The freeze output can be used to recreate environments: docs.python-guide.org/en/latest/dev/virtualenvs/#other-notes
pip freeze and pip list list everything. They don't exclude the packages that weren't installed by pip.
I also find pip freeze to be reliable in scripts, whereas pip list will generate unexpected errors when being used with pipes.
The new (?) pip list -l or pip list --local is the best answer, see stackoverflow.com/a/43012269/491884 by @MJB
G
Glen Selle

As of version 1.3 of pip you can now use pip list

It has some useful options including the ability to show outdated packages. Here's the documentation: https://pip.pypa.io/en/latest/reference/pip_list/


On Gentoo, this lists all the packages, even those installed by other means than pip. Does this really exclude non-pip installed modules on other system as requested by the question?
Note that this is slightly different from pip freeze
pip list --user only shows packages installed by the user, and excludes system-wide packages.
@JacobHume the --user flag makes pip install stuff to the user install directory. it will not list stuff installed with sudo pip install packagexyz.
D
Daniel Sikes

If anyone is wondering you can use the 'pip show' command.

pip show [options] <package>

This will list the install directory of the given package.


n
ng10

Start with:

$ pip list

To list all packages. Once you found the package you want, use:

$ pip show <package-name>

This will show you details about this package, including its folder. You can skip the first part if you already know the package name

Click here for more information on pip show and here for more information on pip list.

Example:

$ pip show jupyter
Name: jupyter
Version: 1.0.0
Summary: Jupyter metapackage. Install all the Jupyter components in one go.
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.org
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires: ipywidgets, nbconvert, notebook, jupyter-console, qtconsole, ipykernel    

Best answer IMHO. Really simple and fast if I know the package name. pip list is not even necessary and freeze can take a long time and I have to filter out stuff I don't care about. Simple: pip show my_packge
C
Capi Etheriel

If Debian behaves like recent Ubuntu versions regarding pip install default target, it's dead easy: it installs to /usr/local/lib/ instead of /usr/lib (apt default target). Check https://askubuntu.com/questions/173323/how-do-i-detect-and-remove-python-packages-installed-via-pip/259747#259747

I am an ArchLinux user and as I experimented with pip I met this same problem. Here's how I solved it in Arch.

find /usr/lib/python2.7/site-packages -maxdepth 2 -name __init__.py | xargs pacman -Qo | grep 'No package'

Key here is /usr/lib/python2.7/site-packages, which is the directory pip installs to, YMMV. pacman -Qo is how Arch's pac kage man ager checks for ownership of the file. No package is part of the return it gives when no package owns the file: error: No package owns $FILENAME. Tricky workaround: I'm querying about __init__.py because pacman -Qo is a little bit ignorant when it comes to directories :(

In order to do it for other distros, you have to find out where pip installs stuff (just sudo pip install something), how to query ownership of a file (Debian/Ubuntu method is dpkg -S) and what is the "no package owns that path" return (Debian/Ubuntu is no path found matching pattern). Debian/Ubuntu users, beware: dpkg -S will fail if you give it a symbolic link. Just resolve it first by using realpath. Like this:

find /usr/local/lib/python2.7/dist-packages -maxdepth 2 -name __init__.py | xargs realpath | xargs dpkg -S 2>&1 | grep 'no path found'

Fedora users can try (thanks @eddygeek):

find /usr/lib/python2.7/site-packages -maxdepth 2 -name __init__.py | xargs rpm -qf | grep 'not owned by any package'

+1 Thanks alot for the Arch one-liner, that's exactly what I was looking for. By the way, curious fact: my Arch installation is localized in italian, but grep correctly 'grepped' the lines that said 'Nessun pacchetto' (italian for 'No package') even though I grepped for 'No package'. How come?
@barraponto @NadirSampaoli grep does nothing in my case. Because grep try to grep in stdout, but dpkg write the error to stderr so i have to add a redirect 2>&1. And for international output add LANG= in front of xargs dpkg -s. and sed is also a nice tool ;) to keep only the package name of the path. So I end up with: find /usr/local/lib/python2.7/dist-packages -maxdepth 2 -name __init__.py | xargs realpath | LANG= xargs dpkg -S 2>&1 | grep 'no path found' | sed "s/.*\/\([^\/]*\)\/__init__\.py.*/\1/"
hi, I downvoted this by mistake and only just realised it, and now my vote's locked in. Could someone vote this up to neutralise my downvote? Thanks
L
Luc

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.


turns out we had pip freeze --local for 8 years. pip list --local is available too... but notice the OP question is not about virtual environments (which --local supports) but about discerning distro packages from sudo pip install packages.
y
yegle

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

This will not work anymore. See github.com/pypa/pip/issues/5243 Instead you should use: import pkg_resources [print(d.project_name) for d in pkg_resources.working_set]
M
MarkHu

The below is a little slow, but it gives a nicely formatted list of packages that pip is aware of. That is to say, not all of them were installed "by" pip, but all of them should be able to be upgraded by pip.

$ pip search . | egrep -B1 'INSTALLED|LATEST'

The reason it is slow is that it lists the contents of the entire pypi repo. I filed a ticket suggesting pip list provide similar functionality but more efficiently.

Sample output: (restricted the search to a subset instead of '.' for all.)

$ pip search selenium | egrep -B1 'INSTALLED|LATEST'

selenium                  - Python bindings for Selenium
  INSTALLED: 2.24.0
  LATEST:    2.25.0
--
robotframework-selenium2library - Web testing library for Robot Framework
  INSTALLED: 1.0.1 (latest)
$

Why not file the ticket yourself?
Apparentlly the next version of pip will have a new list command: pip-installer.org/en/latest/usage.html#pip-list
A
All Іѕ Vаиітy

Adding to @Paul Woolcock's answer,

pip freeze > requirements.txt

will create a requirements file with all installed packages along with the installed version numbers in the active environment at the current location. Running

pip install -r requirements.txt

will install the packages specified in the requirements file.


n
ntk4

Take note that if you have multiple versions of Python installed on your computer, you may have a few versions of pip associated with each.

Depending on your associations, you might need to be very cautious of what pip command you use:

pip3 list 

Worked for me, where I'm running Python3.4. Simply using pip list returned the error The program 'pip' is currently not installed. You can install it by typing: sudo apt-get install python-pip.


pip is for python2.7, pip3 is for python3.x
m
mscheker

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


u
user3013438

pip freeze lists all installed packages even if not by pip/easy_install. On CentOs/Redhat a package installed through rpm is found.


e
eddygeek

Here is the one-liner for fedora or other rpm distros (based on @barraponto tips):

find /usr/lib/python2.7/site-packages -maxdepth 2 -name __init__.py | xargs rpm -qf | grep 'not owned by any package'

Append this to the previous command to get cleaner output:

 | sed -r 's:.*/(\w+)/__.*:\1:'

I
Ignacio Vazquez-Abrams

Get all file/folder names in site-packages/ (and dist-packages/ if it exists), and use your package manager to strip the ones that were installed via package.


C
C S

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.


E
Ed K

For those who don't have pip installed, I found this quick script on github (works with Python 2.7.13):

import pkg_resources
distros = pkg_resources.AvailableDistributions()
for key in distros:
  print distros[key]

r
ron33

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


This is the same as 2nd highest voted answer.
k
keypress

At least for Ubuntu (maybe also others) works this (inspired by a previous post in this thread):

printf "Installed with pip:";
pip list 2>/dev/null | gawk '{print $1;}' | while read; do pip show "${REPLY}" 2>/dev/null | grep 'Location: /usr/local/lib/python2.7/dist-packages' >/dev/null; if (( $? == 0 )); then printf " ${REPLY}"; fi; done; echo