开发者

How can i parse the return value(s) from python -c "import mypackage"

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜