Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
D. E. Scioli Avatar asked Oct 21 '25 12:10

D. E. Scioli


1 Answers

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.

like image 89
Daishi Avatar answered Oct 23 '25 02:10

Daishi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!