未定义 Python 异常类,无属性



我目前正在使用pytesseract,它定义了一个异常类,如下所示:

class TesseractError(Exception):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)

在我的 main.py 中,我尝试了几种方法将此异常用于异常处理,但我没有得到"TesseractError"属性错误和"TesseractError"定义的错误。

1.

>>> from pytesseract import TesseractError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TesseractError'

阿拉伯数字。

>>> import pytesseract
>>> try: raise pytesseract.TesseractError(True,"True")
... except TesseractError: print("error")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pytesseract' has no attribute 'TesseractError'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'TesseractError' is not defined

3.

>>> import pytesseract
>>> try: raise TesseractError(True,"True")
... except TesseractError: print("error")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'TesseractError' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'TesseractError' is not defined

但是,当我在终端中尝试以下内容时,它就可以工作了。

>>> class ERR(Exception): pass
>>> try: raise ERR()
... except ERR: print("error found")
error found

所以似乎导入步骤是错误的,但我不知道是什么原因造成的。

这是__init__文件:

https://github.com/madmaze/pytesseract/blob/master/src/初始化.py

请注意,它不会导入从包本身可见的TesseractError。您可以更改 init 文件或从包>模块导入>

from pytesseract.pytesseract import TesseractError

最新更新