Is there a way to check programmatically, from Python, if the user has setuptools's "easy_install" available or the distribute version of "easy_install"?

I'd like to know which of those is available (and ideally which is "in-use" by the system.)

How can this be done? thanks.

有帮助吗?

解决方案

For easy-install based

>>> import pkg_resources
>>> pkg_resources.get_distribution('setuptools')
setuptools 0.6c11 (t:\tmp\easyinstall\lib\site-packages\setuptools-0.6c11-py2.7.egg)
>>> pkg_resources.get_distribution('setuptools').project_name
'setuptools'

For distribute based

>>> import pkg_resources
>>> pkg_resources.get_distribution('setuptools')
distribute 0.6.31 (t:\tmp\distribute\lib\site-packages\distribute-0.6.31-py2.7.egg)
>>> pkg_resources.get_distribution('setuptools').project_name
'distribute'

其他提示

Use the included pkg_resources library to detect if setuptools is available and if so, what version it is. If you cannot import pkg_resources, there is no setuptools library installed, full stop:

try:
    import pkg_resources
except ImportError:
    print "No setuptools installed for this Python version"
else:
    dist = pkg_resources.get_distribution('setuptools')
    print dist.project_name, dist.version

The project name is either distribute or setuptools; for me this prints:

>>> import pkg_resources
>>> dist = pkg_resources.get_distribution('setuptools')
>>> print dist.project_name, dist.version
distribute 0.6.32

See the Distribution attributes documentation for further details on what information is available.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top