pythonexceptionpyautoguiexcept

Python PyAutoGUI return "ImageNotFoundException", but "except" doesn't recognise it as an exception


With the previous version of pyautogui, when an image wasn't found the return was "None", so I used to handle it with except TypeError:. But since the update (version 0.9.41), it isn't working, as it's returning ImageNotFoundException, but it isn't recognised as an exception. When I try to do except ImageNotFoundException: it gives the error:

[E0712] Catching an exception which doesn't inherit from Exception: ImageNotFoundException

How should this error be handled?


Solution

  • This works fine with pyautogui 0.9.52 and python 3.9.0

    import pyautogui
    
    # force use of ImageNotFoundException
    pyautogui.useImageNotFoundException()
    
    try:
        location = pyautogui.locateOnScreen('foo.png')
        print('image found')
    except pyautogui.ImageNotFoundException:
        print('ImageNotFoundException: image not found')
    

    I found the useImageNotFoundException() part in this page locateCenterOnScreen doesn't raise ImageNotFoundException. · Issue #441 · asweigart/pyautogui. If useImageNotFoundException() is not called, locateOnScreen() just returns None.