Case # 1 : No values are returned , means success
Case # 2: get error:
Traceback (most recent call last): File "", line 1, in ImportError: No module nam开发者_如何学编程ed mypackage
In short, don't.
In longer, if you need to detect what packages are installed, at least do something like:
try:
import mypkg
print "can import"
except:
import "can't import"
Or check the return value:
$ python -c "import foo" 2> /dev/null; echo $? 1 $ python -c "import sys" 2> /dev/null; echo $? 0
精彩评论