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

发布于 2024-11-18 21:54:47 字数 104 浏览 2 评论 0原文

有没有办法找到所有使用 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

つ可否回来 2024-11-25 21:54:47

pip freeze 将输出已安装的软件包及其版本的列表。它还允许您将这些包写入一个文件,稍后可用于设置新环境。

https://pip.pypa.io/en/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

2024-11-25 21:54:47

从 pip 1.3 版本开始,您现在可以使用 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/

岁月蹉跎了容颜 2024-11-25 21:54:47

如果有人想知道你可以使用“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.

寻梦旅人 2024-11-25 21:54:47

开始于:

$ 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: [email protected]
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: [email protected]
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires: ipywidgets, nbconvert, notebook, jupyter-console, qtconsole, ipykernel    
不气馁 2024-11-25 21:54:47

如果 Debian 在 pip install 默认目标方面表现得像最近的 Ubuntu 版本,那就非常简单了:它安装到 /usr/local/lib/ 而不是 /usr/libapt 默认目标)。检查https://askubuntu.com/questions/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 kage ma​​n ager 检查文件所有权的方式。 No package 是当没有包拥有该文件时返回的一部分:error: No package owns $FILENAME。棘手的解决方法:我正在查询__init__.py,因为pacman -Qo在目录方面有点无知:(

为了对其他发行版做到这一点,你必须找出 pip 在哪里安装东西(只是 sudo pip install 东西),如何查询文件的所有权(Debian/Ubuntu 方法是 dpkg -S)以及“没有软件包拥有该路径”的返回值(Debian/Ubuntu 是 没有找到匹配模式的路径 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'
病毒体 2024-11-25 21:54:47

新版本的 pip 能够通过 pip list -lpip freeze -l (--list) 执行 OP 想要的操作。
在 Debian 上(至少),手册页没有明确说明这一点,我只是在假设该功能必须存在的情况下使用 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.

╭⌒浅淡时光〆 2024-11-25 21:54:47

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
后来的我们 2024-11-25 21:54:47

添加@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.

忆梦 2024-11-25 21:54:47

下面的代码有点慢,但它提供了 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)
$
预谋 2024-11-25 21:54:47

请注意,如果您的计算机上安装了多个版本的 Python,则每个版本可能会关联多个 pip 版本。

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

pip3 list 

为我工作,我正在运行 Python3.4。仅使用pip list返回错误程序“pip”当前未安装。您可以通过键入: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.

无名指的心愿 2024-11-25 21:54:47

正如 @almenon 指出的,这不再有效,并且它不是在代码中获取包信息的受支持的方法。以下情况会引发异常:

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

要实现此目的,您可以导入 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

櫻之舞 2024-11-25 21:54:47

pip freeze 列出所有已安装的软件包,即使不是通过 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.

似狗非友 2024-11-25 21:54:47

以下是用于 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:'
落在眉间の轻吻 2024-11-25 21:54:47

获取 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.

A君 2024-11-25 21:54:47

如果您使用 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 安装的条目code> (可能包括 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 uninstall 全部:

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.

滿滿的愛 2024-11-25 21:54:47

对于那些没有安装 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]
假扮的天使 2024-11-25 21:54:47

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

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

紧拥背影 2024-11-25 21:54:47

至少对于 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
回忆躺在深渊里 2024-11-25 21:54:47

pip list:

pip list

这将获取已安装软件包的列表及其尖括号中的版本

pip List 有多个选项,例如

  1. 列出过时的软件包

     python -m pip list --outdated
    

这将列出 python 中安装的所有过时的软件包。

  1. 列出所有更新的包

    python -m pip list -u
    

这将列出所有最新的软件包。

  1. 列出没有依赖项的过时软件包

    python -m pip list --outdated --not-required
    

这将列出所有不依赖于其他包的过时包。

  1. 以json格式列出所有包

    python -m pip list --format=json
    

有关更多详细信息,请参阅:https://www .datasciencemadesimple.com/list-packages-modules-installed-python/

pip freeze:

我们还可以使用

pip freeze

这将获取已安装软件包的列表及其版本,如下所示
输入图片此处描述

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

  1. List outdated packages

       python -m pip list --outdated
    

This will List all outdated packages installed in python.

  1. List all updated packages

    python -m pip list -u
    

This will list all package that are upto date.

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

  1. 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
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文