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

有没有办法找到所有通过easy_install或pip安装的Python PyPI软件包?我的意思是,排除分发工具已经安装的所有东西(在本例中为Debian上的apt-get)。

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


回答 0

pip freeze将输出已安装软件包及其版本的列表。它还允许您将那些程序包写入文件,以便以后用于设置新环境。

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

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


回答 1

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

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

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/


回答 2

如果有人想知道您可以使用“ pip show”命令。

pip show [options] <package>

这将列出给定软件包的安装目录。

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.


回答 3

如果Debian在pip install默认目标上的行为类似于最近的Ubuntu版本,那就太简单了:它安装到(/usr/local/lib/而不是/usr/libapt默认目标))。检查/ubuntu/173323/how-do-i-detect-and-remove-python-packages-installed-via-pip/259747#259747

我是ArchLinux用户,在尝试pip时遇到了同样的问题。这是我在Arch中解决问题的方法。

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

此处的关键是/usr/lib/python2.7/site-packages,这是pip安装到的目录YMMV。pacman -Qo是如何Arch的PAC卡格男人几岁检查该文件的所有权。No package是没有包拥有文件时返回的一部分error: No package owns $FILENAME。整蛊解决方法:我询问有关__init__.py,因为pacman -Qo有点懵,当涉及到目录:(

为了在其他发行版中做到这一点,您必须找出在哪里pip安装东西(仅仅sudo pip install是东西),如何查询文件的所有权(Debian / Ubuntu方法是dpkg -S)以及“没有软件包拥有该路径”返回(Debian)是什么。 / Ubuntu是no path found matching pattern)。Debian / Ubuntu用户,请注意:dpkg -S如果给它一个符号链接,它将失败。只需先使用解决即可realpath。像这样:

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用户可以尝试(感谢@eddygeek):

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

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'

回答 4

从…开始:

$ pip list

列出所有软件包。找到所需的软件包后,请使用:

$ pip show <package-name>

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

点击这里对PIP显示更多的信息,这里的PIP列表的详细信息。

例:

$ 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    

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    

回答 5

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

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

回答 6

下面的程序有点慢,但是它给出了一个可以识别的格式良好的软件包列表pip。也就是说,并不是所有的人都通过“ pip”安装的,但是所有的人都应该能够通过pip进行升级。

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

之所以慢,是因为它列出了整个pypi存储库的内容。我提交了一张票,建议pip list提供类似的功能,但效率更高。

样本输出:(将搜索限制为所有子集而不是“。”。)

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

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

回答 7

除了@Paul Woolcock的答案,

pip freeze > requirements.txt

将在当前位置的活动环境中创建一个包含所有已安装软件包以及已安装版本号的需求文件。跑步

pip install -r requirements.txt

将安装需求文件中指定的软件包。

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.


回答 8

较新版本的pip可以通过pip list -lpip freeze -l--list)执行OP所需的操作。
在Debian上(至少),手册页对此没有明确说明,而我只是在假设该功能必须存在的情况下才发现了with pip list --help

最近有评论表明此功能在文档或现有答案中均不明显(尽管有人暗示),所以我认为应该发布。我本来希望以此作为评论,但我没有信誉点。

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.


回答 9

请注意,如果您的计算机上安装了多个版本的Python,则每个版本可能都有一些pip版本。

根据您的关联,您可能需要非常谨慎使用以下pip命令:

pip3 list 

在运行Python3.4的地方为我工作。简单使用pip list返回错误The program 'pip' is currently not installed. You can install it by typing: sudo apt-get install python-pip

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.


回答 10

正如@almenon指出的那样,这不再起作用,它也不是在代码中获取包信息的支持方式。以下引发异常:

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

为此,您可以import pkg_resources。这是一个例子:

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

我上线了 v3.6.5

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


回答 11

这是Fedora或其他rpm发行版的一线工具(基于@barraponto技巧):

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

将此附加到上一个命令以获取更干净的输出:

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

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:'

回答 12

获取site-packages/dist-packages/如果存在)所有文件/文件夹的名称,然后使用包管理器剥离通过软件包安装的文件/文件夹名称。

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.


回答 13

pip Frozen列出了所有已安装的软件包,即使不是通过pip / easy_install也是如此。在CentOs / Redhat上,找到了通过rpm安装的软件包。

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


回答 14

如果使用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 

最后,您可以获取这些值并使用以下命令pip卸载所有这些值:

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

请注意,使用-y标记pip uninstall来避免必须确认删除。

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.


回答 15

对于那些没有安装pip的人,我在github上找到了这个快速脚本(适用于Python 2.7.13):

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

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]

回答 16

点列表[选项]您可以在此处查看完整的参考

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


回答 17

至少对于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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。