有没有办法找到使用easy_install或pip安装的所有Python PyPI包?我的意思是,不包括发行版工具(在Debian上的apt-get)安装的所有东西。


当前回答

从pip的1.3版本开始,您现在可以使用pip list

它有一些有用的选项,包括显示过期包的能力。下面是文档:https://pip.pypa.io/en/latest/reference/pip_list/

其他回答

从pip的1.3版本开始,您现在可以使用pip list

它有一些有用的选项,包括显示过期包的能力。下面是文档:https://pip.pypa.io/en/latest/reference/pip_list/

pip. get_installed_distributors()将给出已安装包的列表

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

开始:

$ pip list

列出所有软件包。一旦你找到你想要的包,使用:

$ pip show <package-name>

这将显示有关此包的详细信息,包括其文件夹。如果您已经知道包的名称,可以跳过第一部分

点击这里查看更多关于pip show的信息,点击这里查看更多关于pip list的信息。

例子:

$ 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    

如果你使用Anaconda python发行版,你可以使用conda list命令来查看用什么方法安装了什么:

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

获取pip安装的条目(可能包括pip本身):

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>

当然,你可能只想选择第一列,你可以这样做(如果需要,不包括pip):

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

最后,你可以获取这些值,并使用以下命令卸载它们:

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

注意,pip卸载时使用了-y标志,以避免在删除时必须给出确认。

新版本的pip能够通过pip list -l或pip freeze -l(——list)执行OP想要的操作。 在Debian上(至少),手册页并没有说明这一点,我只是在假定该特性必须存在的情况下,通过pip list—help才发现了它。

最近有评论认为这个功能在文档或现有的答案中都不明显(尽管有些人暗示了),所以我认为我应该发布。我更愿意这样做作为一个评论,但我没有声誉点。