M1 Mac的PyCharm中PyAutoGui locateCenterOnScreen函数报错求助
问题描述
在搭载macOS Monterey的M1 MacBook Air上,PyCharm中调用PyAutoGui 0.9.53版本的locateCenterOnScreen()函数时抛出NameError: name 'Image' is not defined错误,但该函数在VS Code中可正常运行,报错回溯如下:
Traceback (most recent call last): File "/Users/username/Desktop/Files/Programming/Projects/Auto Mining Tool/main.py", line 3, in <module> x, y = pyautogui.locateCenterOnScreen('image.png') File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 175, in wrapper return wrappedFunction(*args, **kwargs) File "/Users/username/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 207, in locateCenterOnScreen return pyscreeze.locateCenterOnScreen(*args, **kwargs) File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 413, in locateCenterOnScreen coords = locateOnScreen(image, **kwargs) File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 372, in locateOnScreen screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here. File "/Users/andrewwalker/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 477, in _screenshot_osx im = Image.open(tmpFilename) NameError: name 'Image' is not defined
解决方法
- 确认PyCharm使用的Python环境:打开
File > Settings > Project: [你的项目名] > Python Interpreter,查看当前环境的包列表,确认是否安装了Pillow(Image是Pillow库的核心模块)。 - 若未安装或版本异常,在PyCharm终端执行:
pip install --upgrade pillow - 若仍报错,升级PyAutoGui依赖的
pyscreeze库:pip install --upgrade pyscreeze - 针对M1芯片的适配问题,可尝试强制重装适配arm64的相关包:
pip install --force-reinstall pillow pyscreeze pyautogui - 也可以切换Rosetta模式启动PyCharm:右键应用程序中的PyCharm,选择
显示简介,勾选使用Rosetta打开,重启后再测试。
内容的提问来源于stack exchange,提问作者andreww61004
相关产品推荐
相关产品推荐

