如何在不安装的情况下列出 python 库的依赖项? [英] How to list dependencies for a python library without installing?

查看:34
本文介绍了如何在不安装的情况下列出 python 库的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不先安装的情况下获取给定 python 包的依赖项列表?

我目前可以获得需求列表,但它需要安装软件包.例如,我可以使用 pip 显示基本要求信息,但它不包括版本信息:

$ pip show pytest名称:pytest版本:3.0.6...需要:colorama、setuptools、py

我尝试了一个名为 pipdeptree 包含更好的需求输出,但也需要安装软件包

$ pipdeptree -p pytestpytest==3.0.6- colorama [需要:任何,已安装:0.3.7]- py [需要:>=1.4.29,已安装:1.4.32]- setuptools [需要:任何,已安装:34.0.0]- appdirs [需要:>=1.4.0,已安装:1.4.0]...

理想情况下,我会获得 pipdeptree 提供的详细程度.此外,能够从 python wheel 或从带有 pip 的 pypi 生成 requirements.txt 文件也足够了.

我对给定包的依赖约束感兴趣,而不是解决依赖需求后最终下载的包.例如,我真的不在乎 pip 下载了 package-2.3.4,我宁愿知道 package>=2.1 是一个要求.

解决方案

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

<预><代码>>>>进口请求>>>url = 'https://pypi.org/pypi/{}/json'>>>json = requests.get(url.format('pandas')).json()>>>json['信息']['requires_dist']['numpy (>=1.9.0)', 'pytz (>=2011k)', 'python-dateutil (>=2.5.0)']>>>json['信息']['requires_python']'>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*'

对于特定的包版本,在 URL 中添加一个额外的版本段:

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

Is there a way to get a list of dependencies for a given python package without installing it first?

I can currently get a list of requirements, but it requires installing the packages. For example, I can use pip to show basic requirements info, but it doesn't include version information:

$ pip show pytest
Name: pytest
Version: 3.0.6
...
Requires: colorama, setuptools, py

I've tried a library called pipdeptree that includes much better output on requirements, but it also requires installation of the packages

$ pipdeptree -p pytest
pytest==3.0.6
- colorama [required: Any, installed: 0.3.7]
- py [required: >=1.4.29, installed: 1.4.32]
- setuptools [required: Any, installed: 34.0.0]
  - appdirs [required: >=1.4.0, installed: 1.4.0]
...

Ideally, I would get the level of detail that pipdeptree provides. Also, being able to produce a requirements.txt file from a python wheel or from pypi with pip would suffice as well.

I'm interested in the dependency constraints for a given package, not the final downloaded packages after resolving the dependency requirements. For example, I don't really care that pip downloaded package-2.3.4, I would rather know that package>=2.1 was a requirement.

解决方案

PyPi provides a JSON endpoint with package metadata:

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

For a specific package version, add an additional version segment to the URL:

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

这篇关于如何在不安装的情况下列出 python 库的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆