How to find a Python package's dependencies

2019-01-16 12:46发布

How can you programmatically get a Python package's list of dependencies?

The standard setup.py has these documented, but I can't find an easy way to access it from either Python or the command line.

Ideally, I'm looking for something like:

$ pip install somepackage --only-list-deps
kombu>=3.0.8
billiard>=3.3.0.13
boto>=2.26

or:

>>> import package_deps
>>> package = package_deps.find('somepackage')
>>> print package.dependencies
['kombu>=3.0.8', 'billiard>=3.3.0.13', 'boto>=2.26']

Note, I'm not talking about importing a package and finding all referenced modules. While this might find most of the dependent packages, it wouldn't be able to find the minimum version number required. That's only stored in the setup.py.

标签: python pip
5条回答
ら.Afraid
2楼-- · 2019-01-16 12:46

In addition to the pip show [package name] command, there is pipdeptree.

Just do

$ pip install pipdeptree

then run

$ pipdeptree

and it will show you your dependencies in a tree form, e.g.,

flake8==2.5.0
  - mccabe [required: >=0.2.1,<0.4, installed: 0.3.1]
  - pep8 [required: !=1.6.0,>=1.5.7,!=1.6.1,!=1.6.2, installed: 1.5.7]
  - pyflakes [required: >=0.8.1,<1.1, installed: 1.0.0]
ipdb==0.8
  - ipython [required: >=0.10, installed: 1.1.0]

The project is located at https://github.com/naiquevin/pipdeptree, where you will also find usage information.

查看更多
劫难
3楼-- · 2019-01-16 13:00

Alex's answer is good (+1). In python:

pip._vendor.pkg_resources.working_set.by_key['twisted'].requires()

should return something like

[Requirement.parse('zope.interface>=3.6.0')]

where twisted is the name of the package, which you can find in the dictionary :

pip._vendor.pkg_resources.WorkingSet().entry_keys

to list them all:

dict = pip._vendor.pkg_resources.WorkingSet().entry_keys
for key in dict:
    for name in dict[key]:
        req =pip._vendor.pkg_resources.working_set.by_key[name].requires()
        print('pkg {} from {} requires {}'.format(name,
                                                  key,
                                                  req))

should give you lists like this:

pkg pyobjc-framework-syncservices from /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC requires [Requirement.parse('pyobjc-core>=2.5.1'), Requirement.parse('pyobjc-framework-Cocoa>=2.5.1'), Requirement.parse('pyobjc-framework-CoreData>=2.5.1')]
查看更多
贪生不怕死
4楼-- · 2019-01-16 13:08

Try to use show command in pip, for example:

$ pip show tornado
---
Name: tornado
Version: 4.1
Location: *****
Requires: certifi, backports.ssl-match-hostname

Update (retrieve deps with specified version):

from pip._vendor import pkg_resources


_package_name = 'somepackage'
_package = pkg_resources.working_set.by_key[_package_name]

print([str(r) for r in _package.requires()])  # retrieve deps from setup.py

Output: ['kombu>=3.0.8', 
         'billiard>=3.3.0.13', 
         'boto>=2.26']
查看更多
Deceive 欺骗
5楼-- · 2019-01-16 13:10

All of the above solutions are correct but somewhat inefficient. If your using a MAC, the best way would to be use the 'pip list' command.

MacBook-Pro:~ AIMASTER$ pip list

Hope this helps... Boo YA

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-16 13:13

Try this according to this article in python:

import pip 
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages]) 
print(installed_packages_list)

It will show like:

['behave==1.2.4', 'enum34==1.0', 'flask==0.10.1', 'itsdangerous==0.24', 
 'jinja2==2.7.2', 'jsonschema==2.3.0', 'markupsafe==0.23', 'nose==1.3.3', 
 'parse-type==0.3.4', 'parse==1.6.4', 'prettytable==0.7.2', 'requests==2.3.0',
 'six==1.6.1', 'vioozer-metadata==0.1', 'vioozer-users-server==0.1', 
 'werkzeug==0.9.4']
查看更多
登录 后发表回答