0

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.

Rod
  • 52,748
  • 3
  • 38
  • 55

2 Answers2

2

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'
Rod
  • 52,748
  • 3
  • 38
  • 55
  • How do I parse this? Can just look for "setuptools" versus "distribute" as first word of output? –  Feb 05 '13 at 20:57
  • @user248237 Use the project_name property. I have updated my example – Rod Feb 05 '13 at 21:03
1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343