Python Forum
Cannot unpack non-iterable NoneType object, i would like to ask for help on this.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot unpack non-iterable NoneType object, i would like to ask for help on this.
#1
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 :)
Reply
#2
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
Reply
#3
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.
bowlofred likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 460 Feb-11-2024, 05:38 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 738 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 985 Aug-24-2023, 05:14 PM
Last Post: snippsat
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,460 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,239 Sep-19-2022, 02:43 PM
Last Post: Larz60+
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,561 May-24-2022, 12:03 PM
Last Post: deanhystad
  unpack dict menator01 1 1,183 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,323 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 4,682 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,670 Jan-01-2022, 04:16 PM
Last Post: MaartenRo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020