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).
19 Answers
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
6 Comments
pip freeze and pip list list everything. They don't exclude the packages that weren't installed by pip.pip freeze to be reliable in scripts, whereas pip list will generate unexpected errors when being used with pipes.pip list -l or pip list --local is the best answer, see stackoverflow.com/a/43012269/491884 by @MJBAs 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/
4 Comments
pip freezepip list --user only shows packages installed by the user, and excludes system-wide packages.--user flag makes pip install stuff to the user install directory. it will not list stuff installed with sudo pip install packagexyz.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: [email protected]
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires: ipywidgets, nbconvert, notebook, jupyter-console, qtconsole, ipykernel
1 Comment
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'
3 Comments
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/"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.
1 Comment
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.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
1 Comment
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.
Comments
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)
$
2 Comments
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.
1 Comment
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
Comments
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:'
Comments
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.
Comments
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]
Comments
pip list [options] You can see the complete reference here
1 Comment
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
Comments
pip list:
pip list
This will get the list of installed packages along with their version in angular braces
pip List has multiple options like
List outdated packages
python -m pip list --outdated
This will List all outdated packages installed in python.
List all updated packages
python -m pip list -u
This will list all package that are upto date.
List outdated packages with no dependencies
python -m pip list --outdated --not-required
This will List all outdated packages that are not dependencies of other packages.
List all the packages in json format
python -m pip list --format=json
For More details Refer : https://www.datasciencemadesimple.com/list-packages-modules-installed-python/
pip freeze:
We can Also use
pip freeze
This will get the list of installed packages along with their version as shown below
