首页 > 解决方案 > python中的异常。给定定义,如何在代码中调用它?

问题描述

在库中定义了以下异常:

exception paramiko.ssh_exception.AuthenticationException
Exception raised when authentication failed for some reason. It may be possible to retry with different credentials. (Other classes specify more specific reasons.)

在我的代码中,如果我写:

except AuthenticationException:

我得到一个错误。

如果我写:

except paramiko.AuthenticationException:

有用。

为什么不:

except paramiko.ssh_exception.AuthenticationException:

给定定义,我怎么知道要写什么?

库 paramiko 显然是导入的:

import paramiko

标签: python

解决方案


我猜paramiko包有以下内容__init__.py

from .ssh_exception import *

然后如果你import paramiko,你将拥有所有东西,paramiko而无需额外的子分支


推荐阅读