有没有办法从Python中以编程方式检查用户是否有setuptools的“easy_install”或者“easy_install”的分发版本?
我想知道哪些是可用的(理想情况下系统是“正在使用”)。
如何做到这一点?感谢。
答案 0 :(得分: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'
答案 1 :(得分: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
项目名称为distribute
或setuptools
;对我来说这打印:
>>> import pkg_resources
>>> dist = pkg_resources.get_distribution('setuptools')
>>> print dist.project_name, dist.version
distribute 0.6.32
有关可用信息的更多详细信息,请参阅Distribution
attributes documentation。