You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS下使用PyAutoGUI的locateOnScreen函数报错求助

问题

执行以下PyAutoGUI脚本时触发类型错误:

import pyautogui
res = pyautogui.locateOnScreen('edit')
print(res)

错误回溯:

Traceback (most recent call last):
  File "/Users/adigweleo/gui2.py", line 2, in <module>
    res = pyautogui.locateOnScreen('edit')
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyautogui/__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyautogui/__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyscreeze/__init__.py", line 375, in locateOnScreen
    screenshotIm = screenshot(
                   ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/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'

已尝试操作:

  • 将目标图片保存为edit并放在/Users/adigweleo目录下
  • 创建并激活全新虚拟环境,通过pip/pip3安装了Pillow、pyautogui及pyscreeze,但问题未解决
解决方案

这个错误的核心原因是旧版本pyscreeze处理Pillow版本号时存在逻辑缺陷:当Pillow版本号包含字母后缀(如9.5.0.post1),pyscreeze直接将版本字符串转为元组后,会出现字符串与整数比较的情况,触发类型错误。

解决步骤:

  1. 升级pyscreeze至最新版本,新版已修复该版本号处理逻辑:
pip install --upgrade pyscreeze
  1. 若升级后仍有问题,可安装无后缀的稳定版Pillow:
pip install pillow==9.5.0
  1. 额外注意:locateOnScreen需要传入完整的图片文件名(包含后缀),比如你的图片是PNG格式,需修改代码:
res = pyautogui.locateOnScreen('edit.png')

内容的提问来源于stack exchange,提问作者leo adigwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:22:33