有没有列出 pip 依赖项/需求的方法?

在不进行安装的情况下,我想快速查看 pip install将要安装的所有软件包。

131151 次浏览

注意: 这个答案使用的特征是 已于2014年废弃在2015年被移除。请看适用于现代 pip的其他答案。

直接使用 pip 最接近的方法是使用 --no-install参数:

pip install --no-install <package>

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

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

不可否认,这确实以临时文件的形式留下了一些缺陷,但它确实实现了目标。如果使用 viralenv 执行此操作(应该是这样) ,那么清理过程就像删除 <virtualenv root>/build目录一样简单。

只有在安装了包的情况下,才可以使用 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

正如@radtek 在注释中提到的,应该使用命令 pip install <package> --download <path>,因为从7.0.0(2015-05-21)开始,—— no-install 是来自 pip被移除了。这将把所需的依赖项下载到 <path>中。

这是测试与 pip 版本 8.1.29.0.110.0.118.1

为了获得输出而不打乱你在 Linux 上使用的工作目录

pip download [package] -d /tmp --no-binary :all: -v

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

更好的方法是,使用这个脚本,参数是包名,只获取依赖项作为输出:

#!/bin/sh


PACKAGE=$1
pip download $PACKAGE -d /tmp --no-binary :all:-v 2>&1 \
| grep Collecting \
| cut -d' ' -f2 \
| grep -Ev "$PACKAGE(~|=|\!|>|<|$)"

也有 给你

另一种选择是使用类似于 这个的辅助脚本,它使用 pip.req.parse_requirements API 来解析 requirements.txt文件,使用 distutils.core.setup替换来解析 setup.py文件。

看看我的项目 Johnnydep

安装:

pip install johnnydep

用法例子:

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

更复杂的树:

$ 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

我引用 来自@onnovalkering 的替代解决方案的一句话:

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.*'

对于特定的包版本,向 网址:

https://pypi.org/pypi/pandas/0.22.0/json

此外,如果你正在使用 conda (由@ShpielMeister 建议) ,你可以使用:

conda info package==X.X.X

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

conda info package

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

使用 小树(pip install pipdeptree)。需要安装软件包。

$ pipdeptree -p pandas
pandas==1.2.2
- numpy [required: >=1.16.5, installed: 1.19.5]
- python-dateutil [required: >=2.7.3, installed: 2.8.1]
- six [required: >=1.5, installed: 1.15.0]
- pytz [required: >=2017.3, installed: 2021.1]

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

$ johnnydep pandas
2021-06-09 11:01:21 [info     ] init johnnydist                [johnnydep.lib] dist=pandas parent=None
2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=numpy>=1.16.5 parent=pandas
2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=python-dateutil>=2.7.3 parent=pandas
2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=pytz>=2017.3 parent=pandas
2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=six>=1.5 parent=python-dateutil>=2.7.3
name                        summary
--------------------------  -----------------------------------------------------------------------
pandas                      Powerful data structures for data analysis, time series, and statistics
├── numpy>=1.16.5           NumPy is the fundamental package for array computing with Python.
├── python-dateutil>=2.7.3  Extensions to the standard Python datetime module
│   └── six>=1.5            Python 2 and 3 compatibility utilities
└── pytz>=2017.3            World timezone definitions, modern and historical

我认为这些答案已经过时了,现在有一个更好的解决方案:

要为 setup.cfgsetup.py中的 install_requires中列出的软件包生成 requirements.txt,您需要安装 pip-tools

pip install pip-tools
pip-compile

要生成一个 requirements.txt文件,其中包含 extras_requires下为 testsdev指定的包:

pip-compile --extra tests --extra devrequirements.txt file with packages listed under

此外,您还可以使用 requirements.in文件而不是 setup.cfgsetup.py来列出您的需求。

pip-compile requirements.in

只是一个关于如何使用 johnnydep的附录。

  1. 如果您希望将依赖项设置为 requments.txt 格式:
johnnydep pandas --output-format=pinned
pandas==1.4.1
numpy==1.22.3
python-dateutil==2.8.2
pytz==2021.3
six==1.16.0

上面的列表将写入到 stdout,而收集期间的信息消息将写入到 stderr(如果您想使用 bash 或 subprocess 捕获输出)。

  1. 从 Python 而不是 CLI 调用它:
import sys, johnnydep.cli
sys.argv = ["", "pandas"]
johnnydep.cli.main()