Python Forum
Cannot unpack non-iterable NoneType object, i would like to ask for help on this. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Cannot unpack non-iterable NoneType object, i would like to ask for help on this. (/thread-30371.html)



Cannot unpack non-iterable NoneType object, i would like to ask for help on this. - Jadiac - Oct-18-2020

Hello dear Forum users,
I am currently learning about Pythons Image Recognision and I have encountered this error.
After looking it up online I still cant figure my case out, so I would like to ask for your help on this.

import pyautogui
import keyboard
from time import sleep

while keyboard.is_pressed('m') == False:
    if  pyautogui.locateOnScreen(r'C:\Users\Jadiac\Desktop\Lolbot1\Images\Hud\ShopIcon.png', region=(320, 860, 1320, 1075), grayscale=True, confidence=0.8) != None:
            Shopx, Shopy = pyautogui.locateCenterOnScreen(r'C:\Users\Jadiac\Desktop\Lolbot1\Images\Hud\ShopIcon.png')            
            print("Shopicon location is:", Shopx, Shopy)
            sleep(0.5)
    else:
        print("Cannot find ShopIcon!")

    if  pyautogui.locateOnScreen(r'C:\Users\Jadiac\Desktop\Lolbot1\Images\Hud\spawnPad.png', region=(320, 860, 1320, 1075), grayscale=True, confidence=0.3) != None:
            Spawnx, Spawny = pyautogui.locateCenterOnScreen(r'C:\Users\Jadiac\Desktop\Lolbot1\Images\Hud\spawnPad.png')           
            print("Spawn location is:", Spawnx, Spawny)
            sleep(0.5)
    else:
        print("Cannot find Spawnpad!")
print("done")
Error:
Traceback (most recent call last): File "c:/Users/Jadiac/Desktop/Test/A.py", line 14, in <module> Spawnx, Spawny = pyautogui.locateCenterOnScreen(r'C:\Users\Jadiac\Desktop\Lolbot1\Images\Hud\spawnPad.png') TypeError: cannot unpack non-iterable NoneType object
If anyone could explain this to me and tell me how to counter this I would love to hear any help :)


RE: Cannot unpack non-iterable NoneType object, i would like to ask for help on this. - bowlofred - Oct-18-2020

It means that you called a function (pyautogui.locateCenterOnScreen() in this case) and expected it to return a sequence of data that could be unpacked into two values (which would be assigned to Spawnx and Spawny).

But instead of a sequence of two values, it got back "None".

I'm not familiar with pyautogui to know how to debug that further. If returning None indicates something or if it should have thrown an error or what.

>>> x, y = [1, 2]  # this succeeds
>>> x, y = None    # this fails
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable NoneType object



RE: Cannot unpack non-iterable NoneType object, i would like to ask for help on this. - buran - Oct-18-2020

According to docs:
Quote:The return value is a 4-integer tuple: (left, top, width, height). This tuple can be passed to center() to get the X and Y coordinates at the center of this region. If the image can’t be found on the screen, locateOnScreen() raises ImageNotFoundException.

So, locateOnScreen this should not return None - it should either return 4-element tuple or raise ImageNotFoundException
Even if it returned 4-element tuple, you will be getting a [different] error, because you will try to unpack 4-elememnt tuple in just 2 variables - Too many values to unpack error. I overlooked you use locateCenterOnScreen.

Print to see what locateOnScreen returns, but it's indeed None, so there is probably something else going on here. Is this your actual code, all of it?

EDIT: I found the note right at the start of docs:
Quote:As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None.

So you are using old version of pyautogui.

Given that when you check with low vale for confidence - 0.3 and then enter where you don't supply confidence value, I would guess the default value is greater than 0.3 and you get None. although with low confidence it was returning tuple of coordinates.


RE: Cannot unpack non-iterable NoneType object, i would like to ask for help on this. - Jadiac - Oct-18-2020

Thanks for the help, I figured it out after changing some things and using Imagenotfoundexeption :)