How to list dependencies for a python library without installing?

試著忘記壹切 提交于 2019-12-04 08:59:58

问题


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.

EDIT:

I've looked at similar questions. They are either outdated, require installation, or they don't list individual dependencies for a given package, only a list of 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.


回答1:


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



回答2:


If you don't mind installing conda, this might do the trick for you:

$ conda info numpy=1.11.1 python=3.6.3 

The version numbers of the package or of python are optional (all versions will then be described)




回答3:


Actually, conda gives you two options for this:

conda info {package}
conda install --dry-run {package}

I hear sometimes the latter will install the package if you provide other flags, so I would use the former.



来源:https://stackoverflow.com/questions/41816693/how-to-list-dependencies-for-a-python-library-without-installing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!