有没有办法列出pip依赖/需求?

las*_*ood 74 python pip

如果不进行安装,我想快速查看pip install将要安装的所有软件包.

Jmi*_*lls 86

已接受的答案不再适用于更新的pip版本,并且在不仔细阅读多条评论的情况下不会给出即时答案,因此我提供了更新的答案.

这与PIP测试版本8.1.2,9.0.1,10.0.118.1.

要在Linux上使用,而不会使当前目录混乱

pip download [package] -d /tmp --no-binary :all:
Run Code Online (Sandbox Code Playgroud)

-d 告诉pip下载应放入文件的目录.

更好的是,只需使用此脚本作为包名称参数,仅将依赖项作为输出:

#!/bin/sh

PACKAGE=$1
pip download $PACKAGE -d /tmp --no-binary :all: \
| grep Collecting \
| cut -d' ' -f2 \
| grep -Ev "$PACKAGE(~|=|\!|>|<|$)"
Run Code Online (Sandbox Code Playgroud)

也可以在这里.

  • 这最终会下载并编译所有依赖包,这可能会非常慢...... (6认同)
  • 一些包 *only* 提供二进制文件,所以 `--no-binary :all:` 不是一个好主意。只运送轮子而不运送 sdist 的项目会失败。 (4认同)
  • 请注意,这不会列出已安装的依赖项(这对于 OP 来说很好)。 (2认同)
  • 如果我想对我正在处理的本地包执行此操作该怎么办?例如,我将使用“pip install -e ./mypackage”进行安装?有时我只想知道我正在开发的项目的完整依赖关系树是什么。 (2认同)

wim*_*wim 35

看看johnnydep!

安装:

pip install johnnydep
Run Code Online (Sandbox Code Playgroud)

用法示例:

$ johnnydep requests
name                       summary
-------------------------  ----------------------------------------------------------------------
requests                   Python HTTP for Humans.
??? certifi>=2017.4.17     Python package for providing Mozilla's CA Bundle.
??? chardet<3.1.0,>=3.0.2  Universal encoding detector for Python 2 and 3
??? idna<2.7,>=2.5         Internationalized Domain Names in Applications (IDNA)
??? urllib3<1.23,>=1.21.1  HTTP library with thread-safe connection pooling, file post, and more.
Run Code Online (Sandbox Code Playgroud)

一棵更复杂的树:

$ johnnydep ipython 
name                              summary
--------------------------------  -----------------------------------------------------------------------------
ipython                           IPython: Productive Interactive Computing
??? appnope                       Disable App Nap on OS X 10.9
??? decorator                     Better living through Python with decorators
??? jedi>=0.10                    An autocompletion tool for Python that can be used for text editors.
?   ??? parso==0.1.1              A Python Parser
??? pexpect                       Pexpect allows easy control of interactive console applications.
?   ??? ptyprocess>=0.5           Run a subprocess in a pseudo terminal
??? pickleshare                   Tiny 'shelve'-like database with concurrency support
??? prompt-toolkit<2.0.0,>=1.0.4  Library for building powerful interactive command lines in Python
?   ??? six>=1.9.0                Python 2 and 3 compatibility utilities
?   ??? wcwidth                   Measures number of Terminal column cells of wide-character codes
??? pygments                      Pygments is a syntax highlighting package written in Python.
??? setuptools>=18.5              Easily download, build, install, upgrade, and uninstall Python packages
??? simplegeneric>0.8             Simple generic functions (similar to Python's own len(), pickle.dump(), etc.)
??? traitlets>=4.2                Traitlets Python config system
    ??? decorator                 Better living through Python with decorators
    ??? ipython-genutils          Vestigial utilities from IPython
    ??? six                       Python 2 and 3 compatibility utilities
Run Code Online (Sandbox Code Playgroud)

  • @ so860否,它不需要安装软件包。这就是重点,它可以在隔离的环境中工作。 (4认同)
  • 需要明确的是:安装“johnnydep”本身会安装依赖项。 (4认同)
  • @wim:这个项目真是太棒了!爱它 ! (3认同)
  • @GPHemsley ..它有相当多的依赖项,请参阅“pip show”:“需要:structlog、wheel、setuptools、wimpy、cachetools、anytree、distlib、tabulate、colorama、pip、packaging、toml、pkginfo、oyaml” (3认同)
  • @BenFarmer 这是不正确的。它不安装软件包,而是从 [wheel](https://www.python.org/dev/peps/pep-0427/) 文件读取元数据,这确实需要下载软件包,但不需要安装。在极少数情况下,项目已经发布了源代码发行版,但没有可用的兼容wheel,那么 pip 将尝试从 sdist 生成一个wheel(这是[获取包元数据的唯一可靠方法](https://discuss. python.org/t/sdist-idea-specifying-static-metadata-that-c​​an-be-trusted/4703/12)来自 sdist)。在最常见的情况下,没有安装过程。 (2认同)

Tra*_*ger 16

你可以直接用pip得到的最接近的是使用pip参数:

pip install --no-install <package>
Run Code Online (Sandbox Code Playgroud)

例如,这是安装芹菜时的输出:

Downloading/unpacking celery                                                                                   
  Downloading celery-2.5.5.tar.gz (945Kb): 945Kb downloaded
  Running setup.py egg_info for package celery

    no previously-included directories found matching 'tests/*.pyc'
    no previously-included directories found matching 'docs/*.pyc'
    no previously-included directories found matching 'contrib/*.pyc'
    no previously-included directories found matching 'celery/*.pyc'
    no previously-included directories found matching 'examples/*.pyc'
    no previously-included directories found matching 'bin/*.pyc'
    no previously-included directories found matching 'docs/.build'
    no previously-included directories found matching 'docs/graffles'
    no previously-included directories found matching '.tox/*'
Downloading/unpacking anyjson>=0.3.1 (from celery)
  Downloading anyjson-0.3.3.tar.gz
  Running setup.py egg_info for package anyjson

Downloading/unpacking kombu>=2.1.8,<2.2.0 (from celery)
  Downloading kombu-2.1.8.tar.gz (273Kb): 273Kb downloaded
  Running setup.py egg_info for package kombu

Downloading/unpacking python-dateutil>=1.5,<2.0 (from celery)
  Downloading python-dateutil-1.5.tar.gz (233Kb): 233Kb downloaded
  Running setup.py egg_info for package python-dateutil

Downloading/unpacking amqplib>=1.0 (from kombu>=2.1.8,<2.2.0->celery)
  Downloading amqplib-1.0.2.tgz (58Kb): 58Kb downloaded
  Running setup.py egg_info for package amqplib

Successfully downloaded celery anyjson kombu python-dateutil amqplib
Run Code Online (Sandbox Code Playgroud)

不可否认,这确实会以临时文件的形式留下一些残余,但它确实实现了目标.如果你使用virtualenv(你应该这样做)这样做,清理就像删除--no-install目录一样简单.

  • 在我的pip版本(1.5.4)上,不推荐使用`--no-install`标志. (21认同)
  • 我试过`pip --no-install celery`但我收到错误`没有这样的选项: - no-install`(pip 1.2.1) (12认同)
  • 原因是元数据不存在于setup.py之外,所以与`rpm`或`dpkg`不同,你在顶部构建元数据索引并查询`pip`和`pypi`不起作用那样.所以我们必须通过每个要求. (8认同)
  • 我认为他的意思是'pip install --no-install celery` (4认同)
  • 对于1.5.4,请使用pip install --download =. - 没有用轮子芹菜 (4认同)
  • 我很高兴一个听起来很荒谬的命令,因为“pip install --no-install”已被弃用.. :) (3认同)
  • Pip现在说使用`pip download <packagename>`.他们肯定很满意! (2认同)

pgm*_*ank 13

我引用@onnovalking 的替代解决方案

PyPi 提供带有包元数据的 JSON 端点:

>>> import requests
>>> url = 'https://pypi.org/pypi/{}/json'
>>> json = requests.get(url.format('pandas')).json()
>>> json['info']['requires_dist']
['numpy (>=1.9.0)', 'pytz (>=2011k)', 'python-dateutil (>=2.5.0)']
>>> json['info']['requires_python']
'>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*'
Run Code Online (Sandbox Code Playgroud)

对于特定的软件包版本,请在 URL 中添加额外的版本段:

https://pypi.org/pypi/pandas/0.22.0/json
Run Code Online (Sandbox Code Playgroud)

另外,如果您使用 conda (按照 @ShpielMeister 的建议),您可以使用:

conda info package==X.X.X
Run Code Online (Sandbox Code Playgroud)

显示信息,包括特定版本的依赖项或:

conda info package
Run Code Online (Sandbox Code Playgroud)

显示信息,包括有关该包的所有受支持版本的依赖项。

  • 我投了反对票,因为这个 json 端点不可靠。例如,查看 [`boto3`](https://pypi.org/pypi/boto3/json),requires_dist 为空,但这是一个[当然在元数据中具有依赖项](https://github .com/boto/boto3/blob/develop/setup.py#L16-L20)。 (2认同)

Sar*_*ica 12

当且仅当包安装时,您可以使用pip show <package>.Requires:在输出结尾处查找字段.显然,这会破坏您的要求,但仍然有用.

例如:

$ pip --version
pip 7.1.0 [...]
$ pip show pytest
---
Metadata-Version: 2.0
Name: pytest
Version: 2.7.2
Summary: pytest: simple powerful testing with Python
Home-page: http://pytest.org
Author: Holger Krekel, Benjamin Peterson, Ronny Pfannschmidt, Floris Bruynooghe and others
Author-email: holger at merlinux.eu
License: MIT license
Location: /home/usr/.tox/develop/lib/python2.7/site-packages
Requires: py
Run Code Online (Sandbox Code Playgroud)

  • 这仅显示了直接需求,将缺少所有传递依赖项。它需要安装。所以,它并没有真正回答这个问题。 (3认同)

Flo*_*ian 9

使用pipdeptree ( pip install pipdeptree)。需要安装该包。

\n
$ pipdeptree -p pandas\npandas==1.2.2\n  - numpy [required: >=1.16.5, installed: 1.19.5]\n  - python-dateutil [required: >=2.7.3, installed: 2.8.1]\n    - six [required: >=1.5, installed: 1.15.0]\n  - pytz [required: >=2017.3, installed: 2021.1]\n
Run Code Online (Sandbox Code Playgroud)\n

使用johnnydep ( pip install johnnydep)。速度较慢,因为它下载了软件包的轮子。

\n
$ johnnydep pandas\n2021-06-09 11:01:21 [info     ] init johnnydist                [johnnydep.lib] dist=pandas parent=None\n2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=numpy>=1.16.5 parent=pandas\n2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=python-dateutil>=2.7.3 parent=pandas\n2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=pytz>=2017.3 parent=pandas\n2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=six>=1.5 parent=python-dateutil>=2.7.3\nname                        summary\n--------------------------  -----------------------------------------------------------------------\npandas                      Powerful data structures for data analysis, time series, and statistics\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 numpy>=1.16.5           NumPy is the fundamental package for array computing with Python.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 python-dateutil>=2.7.3  Extensions to the standard Python datetime module\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 six>=1.5            Python 2 and 3 compatibility utilities\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pytz>=2017.3            World timezone definitions, modern and historical\n
Run Code Online (Sandbox Code Playgroud)\n