简体   繁体   English

查找所有使用 easy_install/pip 安装的软件包?

[英]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?有没有办法找到所有使用 easy_install 或 pip 安装的 Python PyPI 包? I mean, excluding everything that was/is installed with the distributions tools (in this case apt-get on Debian).我的意思是,不包括使用分发工具安装/安装的所有内容(在本例中为 Debian 上的 apt-get)。

pip freeze will output a list of installed packages and their versions. pip freeze将 output 列出已安装的软件包及其版本。 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 https://pip.pypa.io/en/stable/reference/pip_freeze/#pip-freeze

As of version 1.3 of pip you can now use pip list从 pip 的 1.3 版开始,您现在可以使用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/这是文档: https://pip.pypa.io/en/latest/reference/pip_list/

If anyone is wondering you can use the 'pip show' command.如果有人想知道您可以使用“pip show”命令。

pip show [options] <package>

This will list the install directory of the given package.这将列出给定 package 的安装目录。

Start with:从...开始:

$ pip list

To list all packages.列出所有包。 Once you found the package you want, use:找到所需的 package 后,请使用:

$ pip show <package-name>

This will show you details about this package, including its folder.这将向您显示有关此 package 的详细信息,包括其文件夹。 You can skip the first part if you already know the package name如果您已经知道 package 名称,则可以跳过第一部分

Click here for more information on pip show and here for more information on pip list.单击此处了解有关 pip 展示的更多信息,单击此处了解有关 pip 列表的更多信息。

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    

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).如果 Debian 表现得像最近的 Ubuntu 版本,关于pip install默认目标,它很容易:它安装到/usr/local/lib/ /usr/lib apt默认目标)。 Check https://askubuntu.com/questions/173323/how-do-i-detect-and-remove-python-packages-installed-via-pip/259747#259747检查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.我是 ArchLinux 用户,当我尝试使用 pip 时,我遇到了同样的问题。 Here's how I solved it in Arch.这是我在 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.这里的关键是/usr/lib/python2.7/site-packages ,这是 pip 安装到 YMMV 的目录。 pacman -Qo is how Arch's pac kage man ager checks for ownership of the file. pacman -Qo是 Arch管理器检查文件所有权的方式。 No package is part of the return it gives when no package owns the file: error: No package owns $FILENAME . No package是它在没有 package 拥有文件时给出的回报的一部分: 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:(棘手的解决方法:我正在查询__init__.py因为pacman -Qo在涉及目录时有点无知:(

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 ). 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拥有该路径”返回(Debian/Ubuntu no path found matching pattern )。 Debian/Ubuntu users, beware: dpkg -S will fail if you give it a symbolic link. Debian/Ubuntu 用户,请注意:如果你给它一个符号链接, dpkg -S将会失败。 Just resolve it first by using realpath .只需先使用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): Fedora 用户可以尝试(感谢@eddygeek):

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

pip.get_installed_distributions() will give a list of installed packages pip.get_installed_distributions()将给出已安装软件包的列表

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

Newer versions of pip have the ability to do what the OP wants via pip list -l or pip freeze -l ( --list ).较新版本的 pip 能够通过pip list -lpip freeze -l ( --list ) 执行 OP 想要的操作。
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 .在 Debian (至少)上,手册页没有说明这一点,我只是在假设该功能必须存在的情况下才发现它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.我更愿意这样做作为评论,但我没有声誉积分。

The below is a little slow, but it gives a nicely formatted list of packages that pip is aware of.下面有点慢,但它提供了pip知道的格式良好的包列表。 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“安装”的,但应该都可以由pip升级。

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

The reason it is slow is that it lists the contents of the entire pypi repo.它慢的原因是它列出了整个 pypi repo 的内容。 I filed a ticket suggesting pip list provide similar functionality but more efficiently.我提交了一张票,建议pip list提供类似的功能但更有效。

Sample output: (restricted the search to a subset instead of '.' for all.)示例 output:(将搜索限制为子集,而不是全部的“.”。)

$ 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)
$

Adding to @Paul Woolcock's answer,添加到@Paul Woolcock 的答案,

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.将安装需求文件中指定的包。

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.请注意,如果您的计算机上安装了多个版本的 Python,则可能有几个版本的 pip 与每个版本相关联。

Depending on your associations, you might need to be very cautious of what pip command you use:根据您的关联,您可能需要对使用的 pip 命令非常谨慎:

pip3 list 

Worked for me, where I'm running Python3.4.为我工作,我正在运行 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 list返回错误The program 'pip' is currently not installed. You can install it by typing: sudo apt-get install python-pip The program 'pip' is currently not installed. You can install it by typing: sudo apt-get install python-pip . The program 'pip' is currently not installed. You can install it by typing: sudo apt-get install python-pip

As @almenon pointed out, this no longer works and it is not the supported way to get package information in your code.正如@almenon 指出的那样,这不再有效,并且它不是在代码中获取 package 信息的受支持方式。 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 .为此,您可以导入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我在v3.6.5

Here is the one-liner for fedora or other rpm distros (based on @barraponto tips):这是 Fedora 或其他 rpm 发行版的单行代码(基于 @barraponto 提示):

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: Append 这到前面的命令以获得更清洁的 output:

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

pip freeze lists all installed packages even if not by pip/easy_install. pip freeze 会列出所有已安装的软件包,即使不是通过 pip/easy_install。 On CentOs/Redhat a package installed through rpm is found.在 CentOs/Redhat 上找到了一个通过 rpm 安装的 package。

If you use the Anaconda python distribution, you can use the conda list command to see what was installed by what method:如果使用Anaconda python 发行版,可以使用 conda 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

To grab the entries installed by pip (including possibly pip itself):要获取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>

Of course you probably want to just select the first column, which you can do with (excluding pip if needed):当然,您可能只想 select 第一列,您可以使用它(如果需要,不包括pip ):

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:最后,您可以获取这些值,然后 pip 使用以下命令将它们全部卸载:

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.请注意在pip uninstall中使用-y标志,以避免必须确认删除。

For those who don't have pip installed, I found this quick script on github (works with Python 2.7.13):对于那些没有安装 pip 的人,我在 github 上找到了这个快速脚本(适用于Python 2.7.13):

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

pip list [options] You can see the complete reference here pip 列表 [选项] 你可以在这里看到完整的参考

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.获取site-packages/ (和dist-packages/如果存在)中的所有文件/文件夹名称,并使用 package 管理器删除通过 package 安装的文件/文件夹名称。

At least for Ubuntu (maybe also others) works this (inspired by a previous post in this thread):至少对于 Ubuntu (也许还有其他人)可以这样做(受此线程上一篇文章的启发):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM