2013-02-05 34 views
0

有没有一种方法可以从Python中以编程方式检查用户是否有可用的setuptools“easy_install”或分发版本的“easy_install”?从Python检查哪个easy_install版本可用?

我想知道其中哪些是可用的(理想情况下,这是“使用中”的系统。)

如何才能做到这一点?谢谢。

回答

2

为了便于安装基于

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

对于分销基于

>>> 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' 
+0

我该如何解析?只能查找“setuptools”与“distribute”作为输出的第一个单词吗? – user248237dfsf

+0

@ user248237使用project_name属性。我更新了我的例子 – Rod

1

使用随附pkg_resources library来检测,如果setuptools可用,如果是这样,它是什么版本。如果你不能导入pkg_resources,没有setuptools库安装,句号:

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 

的项目名称是distributesetuptools;为我打印:

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

有关可用信息的更多详细信息,请参阅Distribution attributes documentation

相关问题