NameError: 找不到 ObjC 类 'b'NSEvent'' - PyAutoGui

问题描述

我最近安装了 pyautogui,使用 pip3 install pyautogui。这有效,我在安装时没有收到任何错误。但是当如下图导入模块时。

import pyautogui

收到这条错误消息后,我受到了亲切的欢迎。

Traceback (most recent call last):
  File "a.py",line 2,in <module>
    import pyautogui
  File "/Users/name/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pyautogui/__init__.py",line 241,in <module>
    import mouseinfo
  File "/Users/name/.pyenv/versions/3.8.2/lib/python3.8/site-packages/mouseinfo/__init__.py",line 107,in <module>
    NSEvent = Objcclass('NSEvent')
  File "/Users/name/.pyenv/versions/3.8.2/lib/python3.8/site-packages/rubicon/objc/api.py",line 924,in __new__
    ptr,name = cls._new_from_name(name_or_ptr)
  File "/Users/name/.pyenv/versions/3.8.2/lib/python3.8/site-packages/rubicon/objc/api.py",line 827,in _new_from_name
    raise NameError("ObjC Class '%s' Couldn't be found." % name)
NameError: ObjC Class 'b'NSEvent'' Couldn't be found.

在这里做错了什么?我没有正确安装它吗?似乎 docs 像我一样导入模块。

解决方法

我在 macOS Big Sur 11.4 (20F71) 上运行 Python 3.6.8 时遇到了同样的问题。安装正常,没有错误,但在导入 pyautogui 时出现相同的异常。

但是,我尝试安装并运行 Python 3.8.8 没有问题。

我之前在尝试 pip3 install pyautogui 时遇到错误,不得不摆弄它直到一切都说它安装成功。为了确保我的 python 环境没有问题,我为 3.6 创建了一个新的 venv 并遇到了同样的问题。具体来说,它在尝试安装 pyobj 时似乎令人窒息。我所有的软件都是最新的,因为我刚刚更新了一些之前说的所有内容。当前 XCode 版本为 12.5,构建 12E262

    clang -DNDEBUG -g -fwrapv -O3 -Wall -I/usr/local/include -L/usr/local/lib -I/usr/local/include -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/sqlite/include -I/Users/lpettit/Desktop/Luke/venv/include -I/usr/local/Cellar/python@3.6.8/3.6.8_5/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c Modules/objc/module.m -o build/temp.macosx-11-x86_64-3.6/Modules/objc/module.o -g -fexceptions -Wall -Wstrict-prototypes -Wmissing-prototypes -Wformat=2 -W -Wpointer-arith -Wmissing-declarations -Wnested-externs -W -Wno-import -Wno-unknown-pragmas -Wshorten-64-to-32 -Werror -I/usr/include/ffi -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -DPyObjC_BUILD_RELEASE=1103
    clang: error: argument unused during compilation: '-L/usr/local/lib' [-Werror,-Wunused-command-line-argument]
    error: command 'clang' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/lpettit/Desktop/Luke/venv/bin/python3.6 -u -c 'import io,os,sys,setuptools,tokenize; sys.argv[0] = '"'"'/private/var/folders/zm/vmrhx84s2033swkpff49kn_40000gn/T/pip-install-deunlwzj/pyobjc-core_d8cfbbc58c52474aa7935601a9dd5cd8/setup.py'"'"'; __file__='"'"'/private/var/folders/zm/vmrhx84s2033swkpff49kn_40000gn/T/pip-install-deunlwzj/pyobjc-core_d8cfbbc58c52474aa7935601a9dd5cd8/setup.py'"'"';f = getattr(tokenize,'"'"'open'"'"',open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"','"'"'\n'"'"');f.close();exec(compile(code,__file__,'"'"'exec'"'"'))' install --record /private/var/folders/zm/vmrhx84s2033swkpff49kn_40000gn/T/pip-record-5iunioef/install-record.txt --single-version-externally-managed --compile --install-headers /Users/lpettit/Desktop/Luke/venv/include/site/python3.6/pyobjc-core Check the logs for full command output.

如果可能,我建议迁移到 python3.8 或 3.9。我不是 100% 相信 xcode 版本确实是问题,而不仅仅是症状,但您可以尝试回滚您的 c 编译器版本,看看是否有帮助。

,

嗯。我试了一下。

import AppKit
import pyautogui

没有出现错误信息。 我测试了这个答案。

import AppKit
import pyautogui
screenWidth,screenHeight = pyautogui.size()
print((screenWidth,screenHeight))

我没有出错并且收到了正确的输出。

(1440,900)