`keyboard` module needs root

Question: How do I use the ‘keyboard’ module in Python without errors?

Repl link: https://replit.com/@PythonAnaconda/Apocalypse

I’m getting a very long error thread when trying to use the import keyboard.read_key()
Can someone please tell me how to resolve this?

File "/home/runner/Apocalypse/main.py", line 36, in <module>
    title()
  File "/home/runner/Apocalypse/main.py", line 33, in title
    k = keyboard.read_key()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 935, in read_key
    event = read_event(suppress)
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 924, in read_event
    hooked = hook(queue.put, suppress=suppress)
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 461, in hook
    append(callback)
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/_generic.py", line 67, in add_handler
    self.start_if_necessary()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
    self.init()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 196, in init
    _os_keyboard.init()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixkeyboard.py", line 113, in init
    build_device()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixkeyboard.py", line 109, in build_device
    ensure_root()
  File "/home/runner/Apocalypse/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixcommon.py", line 174, in ensure_root
    raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.

Given the error I’d suggest you change the library to use the curses module. It doesn’t require root access and works well on Replit.

There is a good documentation here: Curses Programming with Python — Python 3.12.0 documentation

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.