22

我想import在找不到参数exception时发生这种情况。我正在尝试向库中添加一些额外的功能,但在这一点上我很难过。boto3 ssmget_parameterssmmoto

>>> import boto3
>>> ssm = boto3.client('ssm')
>>> try:
        ssm.get_parameter(Name='not_found')
    except Exception as e:
        print(type(e))
<class 'botocore.errorfactory.ParameterNotFound'>
>>> from botocore.errorfactory import ParameterNotFound
ImportError: cannot import name 'ParameterNotFound'
>>> import botocore.errorfactory.ParameterNotFound
ModuleNotFoundError: No module named 'botocore.errorfactory.ParameterNotFound'; 'botocore.errorfactory' is not a package

但是,Exception无法导入,并且在 botocore 代码中似乎不存在。如何导入此异常?

4

2 回答 2

28
mc = boto3.client('ssm')
try:
  ...
except mc.exceptions.ParameterNotFound:
  ...
于 2018-06-21T00:10:02.817 回答
18

Botocore 错误处理

import boto3
from botocore.exceptions import ClientError

ssm = boto3.client('ssm')
try:
    ssm.get_parameter(Name='not_found')
except ClientError as e:
    print(e.response['Error']['Code'])
于 2017-09-05T21:42:56.350 回答