【发布时间】:2012-06-24 05:58:30
【问题描述】:
在不进行安装的情况下,我想快速查看pip install 将安装的所有包。
【问题讨论】:
-
另一个问题,包括此问题的替代答案:stackoverflow.com/questions/41816693/…
标签: python pip requirements.txt
在不进行安装的情况下,我想快速查看pip install 将安装的所有包。
【问题讨论】:
标签: python pip requirements.txt
接受的答案不再与最新版本的 pip 相关,并且在不仔细阅读多个 cmets 的情况下不会立即给出答案,因此我提供了更新的答案。
这是用 pip 版本 8.1.2、9.0.1、10.0.1 和 18.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(~|=|\!|>|<|$)"
也可以使用here。
【讨论】:
requirements.txt 的非常(very)粗略解读:< requirements.txt egrep -v "^#" | egrep -v "^$" | xargs -L 1 -I % sh -c 'echo %; echo "======"; ./deps.sh %; echo "";
--no-binary :all:不是一个好主意。只提供 wheel 而不是 sdist 的项目会失败。
查看我的项目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
【讨论】:
pip show: Requires: structlog, wheel, setuptools, wimpy, cachetools, anytree, distlib, tabulate, colorama, pip, packaging, toml, pkginfo, oyaml
当且仅当安装包时,您可以使用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
【讨论】:
注意:此答案中使用的功能是deprecated in 2014 和removed in 2015。请查看适用于现代
pip的其他答案。
您可以直接使用 pip 最接近的是使用 --no-install 参数:
pip install --no-install <package>
例如,这是安装 celery 时的输出:
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
诚然,这确实以临时文件的形式留下了一些麻烦,但它确实实现了目标。如果您使用 virtualenv(您应该这样做)执行此操作,那么清理就像删除 <virtualenv root>/build 目录一样简单。
【讨论】:
rpm 或 dpkg 在上面构建元数据索引并查询 pip和pypi 不能那样工作。所以我们必须忽略每一个要求。
pip --no-install celery,但收到错误no such option: --no-install (pip 1.2.1)
pip install --no-install celery
--no-install 标志已弃用。
我引用alternative solution from @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 (as suggested by @ShpielMeister),你可以使用:
conda info package==X.X.X
显示信息,包括特定版本的依赖项或:
conda info package
显示信息,包括该软件包所有受支持版本的依赖关系。
【讨论】:
boto3,requires_dist 为空,但这是certainly has dependencies in the metadata 的项目。
使用pipdeptree (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.cfg 或setup.py 中install_requires 中列出的包生成requirements.txt,您需要安装pip-tools。
pip install pip-tools
pip-compile
要生成一个requirements.txt 文件,其中包含在extras_requires 下为tests 和dev 指定的包:
pip-compile --extra tests --extra devrequirements.txt file with packages listed under
此外,您还可以使用requirements.in 文件而不是setup.cfg 或setup.py 来列出您的要求。
pip-compile requirements.in
【讨论】:
另一种选择是使用类似于this one 的帮助脚本,它使用pip.req.parse_requirements API 来解析requirements.txt 文件并使用distutils.core.setup 替换来解析setup.py 文件。
【讨论】: