drerD
drerD

Reputation: 689

Python exception class not defined, no attribute

I am currently using pytesseract which defines an exception class as follows:

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

in my main.py, I tried several ways to use this exception for exception handling but I've gotten no "TesseractError" attribute error and no "TesseractError" defined error.

1.

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

2.

>>> 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

However, when I try the following in terminal, it just works.

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

so it seems it's the importing step is wrong but I don't know what caused it.

Upvotes: 0

Views: 1558

Answers (1)

JacobIRR
JacobIRR

Reputation: 8946

Here's the __init__ file:

https://github.com/madmaze/pytesseract/blob/master/src/init.py

Notice that it does not import the TesseractError to be visible from the package itself. You could either change the init file or import from the package>module>

from pytesseract.pytesseract import TesseractError

Upvotes: 2

Related Questions