2013-02-05 29 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

相關問題