Search code examples
pythonimporterrorpyautogui

NameError: ObjC Class 'b'NSEvent'' couldn't be found. - PyAutoGui


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.


Solution

  • 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)