Search code examples
python-3.xmacoskeyboard

Python Keyboard Module OsError13


I am trying to learn all about python. So naturally I installed some packages (bumpy, pandas, sicpy, and keyboard). I was playing around with the keyboard package but I kept getting OSError 13. I've seen people have this same problem but no one has given a definite answer on how to fix this.

I looked around on GitHub and stuff. There was a thread on GitHub from 2018 with the same issue but, again there was no definite answer.

Here is the Error Message I keep getting:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keyboard/__init__.py", line 292, in listen
    _os_keyboard.listen(self.direct_callback)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keyboard/_darwinkeyboard.py", line 430, in listen
    raise OSError("Error 13 - Must be run as administrator")
OSError: Error 13 - Must be run as administrator

Here is the actual program code:

import keyboard  # using module keyboard
while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
            break  # finishing the loop
        else:
            pass
    except:
        break  # if user pressed a key other than the given key the loop will break

The error message is in the code. I am also a noob so if this has been answered, if someone could link to that answer. Also: I'm running this with IDLE on Mac OS.


Solution

  • in Terminal, your code should work with

    sudo python3 your_script.py