5

I installed pyautogui recently, using pip3 install pyautogui. This worked and I received no error on installation. But when importing the module as shown below.

import pyautogui

I was kindly greeted with this error message.

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.

What am I doing wrong here? Did I not install it correctly? Seems like the docs import the module like I am.

Buddy Bob
  • 5,829
  • 1
  • 13
  • 44

2 Answers2

7

Um. I gave the following a try.

import AppKit
import pyautogui

No error message came. I tested this answer.

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

I got no error and received the correct output.

(1440, 900)
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
1

I'm having the same issue when running Python 3.6.8 on macOS Big Sur 11.4 (20F71). Installed fine with no errors, but getting the same exception when importing pyautogui.

However, I tried installing and running with Python 3.8.8 had no problems.

I was getting errors when attempting to pip3 install pyautogui previously, and had to fiddle with it until everything said it installed successfully. To be sure it wasn't some issue with my python environment, i created a new venv for 3.6 and got the same issues. Specifically, it seems to be choking when attempting to install pyobj. All my software is current as I just updated everything a few says ago. Current XCode version is 12.5, build 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.

I'd suggest to migrate to python3.8 or 3.9 if possible. I'm not 100% convinced that the xcode version is really the problem and not just a symptom, but you could try and rollback your c compiler version and see if it helps.