0

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.

3
  • 2
    That exception isn't exposed at the top level of the package, only image_to_string is. If you want it, you'll have to from pytesseract.pytesseract import TesseractError. Commented May 16, 2017 at 21:57
  • It worked! How does top-level exposure work here? Commented May 16, 2017 at 21:59
  • I'm not sure what you mean. What is available directly from the package is defined by __init__.py, everything else must be accessed explicitly by module. Commented May 16, 2017 at 22:00

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Note that your import * will be affected by __all__.
ah, just saw that - they only put image_to_string there too :/
so my understanding is when I called "import pytesseract", "____init____" is called and only "from pytesseract import image_to_string" is executed. And when I try "from pytesseract.pytesseract import TesseractError", I am importing TesseractError class from pytesseract.py of pytesseract folder/library. Is that right?
That's correct. It all comes down to the distinction between packages and modules: softwareengineering.stackexchange.com/questions/111871/…
Thanks guys! it helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.