开发者

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

开发者 https://www.devze.com 2023-02-26 18:44 出处:网络
Case # 1 : No values are returned , means success Case # 2: get error: Traceback (most recent call last):

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

精彩评论

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

关注公众号