Issue
I'm using the keyboard module thing and it comes up with this error.
I have seen other posts and have done as they say and used sudo pip3 install keyboard yet it still does not work.
import keyboard
loop = ""
while loop == "":
if keyboard.read_key() == "w":
print("You pressed w")
if keyboard.read_key() == "a":
print("You pressed a")
if keyboard.read_key() == "s":
print("You pressed s")
if keyboard.read_key() == "d":
print("You pressed d")
>>>
==================== RESTART: /home/pi/Desktop/turret.py ====================
Traceback (most recent call last):
File "/home/pi/Desktop/turret.py", line 7, in <module>
if keyboard.read_key() == "w":
File "/usr/local/lib/python3.7/dist-packages/keyboard/__init__.py", line 935, in read_key
event = read_event(suppress)
File "/usr/local/lib/python3.7/dist-packages/keyboard/__init__.py", line 924, in read_event
hooked = hook(queue.put, suppress=suppress)
File "/usr/local/lib/python3.7/dist-packages/keyboard/__init__.py", line 461, in hook
append(callback)
File "/usr/local/lib/python3.7/dist-packages/keyboard/_generic.py", line 67, in add_handler
self.start_if_necessary()
File "/usr/local/lib/python3.7/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.7/dist-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/usr/local/lib/python3.7/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.7/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/usr/local/lib/python3.7/dist-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.
>>>
I run the code and get that error. I thought i did install it to root.
Solution
When you need to run your script as root it's meant to get started like sudo python3 ./yourfile.py
Don't forget to make the script executable by chmod +x.
Answered By - Heini