如何在Python中捕获ImportError异常?
无法导入模块或模块成员时,将引发ImportError。在两种情况下,可能会引发ImportError。
如果模块不存在。
示例
import sys
try:
from exception import myexception
except Exception as e:
print e
print sys.exc_type输出结果
No module named exception <type 'exceptions.ImportError'>
如果从X导入Y,并且在模块X内找不到Y,则会引发ImportError。
示例
import sys
try:
from time import datetime
except Exception as e:
print e
print sys.exc_type输出值
cannot import name datetime <type 'exceptions.ImportError'>
热门推荐
5 怎么说祝福语简短
10 特别的端午祝福语简短
11 分别祝福语简短情侣短句
12 长辈钻石婚祝福语简短
13 中秋祝福语送朋友简短
14 简短56岁生日祝福语
15 女儿生日祝福语说说简短
16 送灯祝福语简短独特
17 大雪生日祝福语大全简短
18 跨年的生日祝福语简短