How to fix 'TypeError: '<' not supported between instances of 'str' and 'int' when running pyautogui.locateOnScreen()?

huangapple go评论75阅读模式
英文:

How to fix 'TypeError: '<' not supported between instances of 'str' and 'int' when running pyautogui.locateOnScreen()?

问题

I'm writing a program that would be able to detect the shiny sprite of whatever pokemon that I'm hunting. However, whenever I run pyautogui.locateOnScreen(), I raise a TypeError: '<' not supported between instances of 'str' and 'int'.

import pyautogui as pag
pag.locateOnScreen('beldum.png')

I also took the screenshot of the image myself, but I heard that using pyautogui's own screenshot function is better. I tried doing this, but I get the same type error whenever I try to do it.

Here are the tracebacks that I got.

Traceback (most recent call last):
  File "/path/to/file", line 3, in <module>
    pag.locateOnScreen('beldum.png')
  File "/path/to/venv/lib/python3.11/site-packages/pyautogui/__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
  File "/path/to/venv/lib/python3.11/site-packages/pyautogui/__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
  File "/path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py", line 375, in locateOnScreen
    screenshotIm = screenshot(
  File "/path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py", line 527, in _screenshot_osx
    if tuple(PIL__version__) < (6, 2, 1):
TypeError: '<' not supported between instances of 'str' and 'int'
英文:

im writing a program that would be able to detect the shiny sprite of whatever pokemon that im hunting, however, whenever i run pyautogui.locateOnScreen() i raise a TypeError: '&lt;' not supported between instances of 'str' and 'int'

import pyautogui as pag
pag.locateOnScreen(&#39;beldum.png&#39;)

i also took the screenshot of the image myself, but i heard that using pyautogui's own screenshot function is better. i tried doing this, but i get the same type error whenever i try to do it.

Here are the tracebacks that I got.

Traceback (most recent call last):
  File &quot;/path/to/file&quot;, line 3, in &lt;module&gt;
    pag.locateOnScreen(&#39;beldum.png&#39;)
  File &quot;/path/to/venv/lib/python3.11/site-packages/pyautogui/__init__.py&quot;, line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/path/to/venv/lib/python3.11/site-packages/pyautogui/__init__.py&quot;, line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py&quot;, line 375, in locateOnScreen
    screenshotIm = screenshot(
                   ^^^^^^^^^^^
  File &quot;/path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py&quot;, line 527, in _screenshot_osx
    if tuple(PIL__version__) &lt; (6, 2, 1):

TypeError: &#39;&lt;&#39; not supported between instances of &#39;str&#39; and &#39;int&#39;

答案1

得分: 19

I had the same problem when using pyautogui. I'm using Python 3.8, and the version of pyautogui is 9.5.0. Actually, this is a bug in the Python framework packages. I don't know if Python 3.11 works the same way. Anyway, this is how I met this problem and fixed it.

I have this error report:

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyscreeze/__init__.py", line 528, in _screenshot_osx
    if tuple(PIL__version__) < (6, 2, 1):
TypeError: '<' not supported between instances of 'str' and 'int'

Then I clicked into this report file and and trace back the value of PIL__version__. It's defined like this: __version__ = "9.5.0"

Now we find the problem. A simple test program can tell you everything:

__version__ = "9.5.0"
print(tuple(__version__))
print(type(tuple(__version__)))
print((6, 2, 1))
print(type((6,2,1)))

The output you'll get looks like this:

('9', '.', '5', '.', '0')
<class 'tuple'>
(6, 2, 1)
<class 'tuple'>

They are both tuples, but the elements inside this tuple are str and int types. Of course they can't be compared.

So you need to click into the reported error file. Replace this code:

if tuple(PIL__version__) < (6, 2, 1):

with this code:

if tuple(map(int, PIL__version__.split("."))) < (6, 2, 1):

Now everything should work fine.

英文:

I had the same problem when using pyautogui. I'm using Python 3.8, and the version of pyautogui is 9.5.0. Actually, this is a bug in the Python framework packages. I don't know if Python 3.11 works the same way. Anyway, this is how I met this problem and fixed it.

I have this error report:

File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyscreeze/__init__.py&quot;, line 528, in _screenshot_osx
    if tuple(PIL__version__) &lt; (6, 2, 1):
TypeError: &#39;&lt;&#39; not supported between instances of &#39;str&#39; and &#39;int&#39;

Then I clicked into this report file and and trace back the value of PIL__version__. It's defined like this: __version__ = &quot;9.5.0&quot;

Now we find the problem. A simple test program can tell you everything:

__version__ = &quot;9.5.0&quot;
print(tuple(__version__))
print(type(tuple(__version__)))
print((6, 2, 1))
print(type((6,2,1)))

The output you'll get looks like this:

(&#39;9&#39;, &#39;.&#39;, &#39;5&#39;, &#39;.&#39;, &#39;0&#39;)
&lt;class &#39;tuple&#39;&gt;
(6, 2, 1)
&lt;class &#39;tuple&#39;&gt;

They are both tuples, but the elements inside this tuple are str and int types. Of course they can't be compared.

So you need to click into the reported error file. Replace this code:

if tuple(PIL__version__) &lt; (6, 2, 1):

with this code:

if tuple(map(int, PIL__version__.split(&quot;.&quot;))) &lt; (6, 2, 1):

Now everything should work fine.

答案2

得分: 7

你也可以在启动时对其进行修补:

import pyscreeze
import PIL

__PIL_TUPLE_VERSION = tuple(int(x) for x in PIL.__version__.split("."))
pyscreeze.PIL__version__ = __PIL_TUPLE_VERSION

<您的应用程序的其余部分>

免责声明:这对某些边缘情况可能不起作用,并且也可能因为pyscreeze的代码更改而导致故障。

英文:

You can also patch it on startup :

import pyscreeze
import PIL

__PIL_TUPLE_VERSION = tuple(int(x) for x in PIL.__version__.split(&quot;.&quot;))
pyscreeze.PIL__version__ = __PIL_TUPLE_VERSION

&lt;rest of your app&gt;

Disclaimer this won't work on some edge cases. and can also break due to code changes on pyscreeze

答案3

得分: 0

  1. 打开位于路径 /path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py__init__.py 文件。

  2. 将第 527 行的代码替换为:

    if tuple([int(x) for x in PIL__version__.split(".")]) < (6, 2, 1):
    
  3. 保存文件。

我遇到了同样的问题,这些步骤对我有用。希望对你也有帮助。

英文:

You can try this following

1.Open a __init__.py in your path &quot;/path/to/venv/lib/python3.11/site-packages/pyscreeze/__init__.py&quot;.
On the Spyder or the other.

2.You just replace this code in line 527

if tuple(PIL__version__) &lt; (6, 2, 1):

with

if tuple([int(x) for x in PIL__version__.split(&quot;.&quot;)])&lt; (6, 2, 1):

3.Save the file.

I have the same problem and these step work for me. Hope it's useful.

huangapple
  • 本文由 发表于 2023年5月30日 09:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定