22

I'm trying to use the keyboard library in python 3 but continue to get an import error. I ran the program in windows in Thonny and It worked fine but I cant run it in the pi. I tried running it both as root and with sudo command with the same results. Below is the code as well as the error.

import keyboard
import time

x=0

while True:
    print (x)
    x=x+1
    print ("Press t to end program")
    if keyboard.is_pressed('t'):
        break
    else:
        pass

print("I'm done")

Output

0
Press t to end program
Traceback (most recent call last):
File "/home/pi/Desktop/Programs/KeyboardTest.py", line 10, in <module>
if keyboard.is_pressed('t'):
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/__init__.py", line 166, in is_pressed
_listener.start_if_necessary()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/__init__.py", line 116, in init
_os_keyboard.init()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_nixkeyboard.py", line 110, in init
build_device()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_nixkeyboard.py", line 106, in build_device
ensure_root()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_nixcommon.py", line 165, 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.
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
vinnie
  • 221
  • 1
  • 2
  • 5
  • 2
    Have a look at the code of keyboard lib: https://github.com/boppreh/keyboard/blob/master/keyboard/_nixcommon.py def ensure_root(): if os.geteuid() != 0: raise ImportError('You must be root to use this library on linux.') And geteuid retrieves the effective user id os.geteuid() Return the current process’s effective user id. So for some reason in your case geteuid returns a value != 0 Write a test program which returns os.geteuid() and fiddle around until it prompts 0 – Omni Feb 14 '18 at 21:15
  • Do you get the exact same error message when you do `sudo python KeyboardTest.py`? – FlyingTeller Feb 14 '18 at 21:16
  • Yes, the same results when I run it as sudo. and just to be clear the erroro message is the output and the code in the error message is in the "keyboard" library. – vinnie Feb 15 '18 at 04:13
  • 1
    I have the same problem ... any solution ? – Prokop Hapala Aug 18 '18 at 07:03

7 Answers7

13

Very late response, but I had your same problem. Just found a solution. You have to be root to run this program! The "Gotcha" comes, however, to how you installed the keyboard library...

Make sure when you installed the keyboard library that you did a:

sudo pip3 install keyboard

I did not do a sudo and first time I installed it. So what happened is that:

1) you try to run the program without being root

  • the library DOES exist, but you are not root so you get the issue you got

2) you try to run teh program as root

  • But pip3 did not install it for a root user, so you get a "keyboard not recognized".

hope this helped.

MS-87
  • 173
  • 1
  • 10
  • 4
    Tbh it seems like this would be pretty clear to most people... "you must run as root"... `sudo...` lol... But I'm wondering if there's an alternative where it doesn't force me to run as root on Linux? Preferably one that doesn't rely on an X server... – 255.tar.xz Oct 29 '19 at 23:58
3

The pynput module avoids the permissions issue altogether.

I-CubeX
  • 41
  • 4
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 22 '22 at 03:49
  • 2
    This is the way. See https://pypi.org/project/pynput/ for their example. – Alex Kaszynski Feb 17 '23 at 17:52
1

On raspberry pi open terminal and go the code folder. Type below command

sudo python filename.py

that's it. If you try to run code with IDLE it will always show this root related error.

1

Another solution. One thing is we want to run the IDLE as root permission.

  1. Open terminal. Type command 'sudo idle'. It will create a new window of IDLE which has permission of root.
  2. Now on that opened IDLE window FILE-> Open -> open the python file that you want to run.
  3. Now go to Run-> Run Module.

Now the python script will run with root permission, as by typing 'sudo idle' we run the IDLE with root permissions.

1

It turns out there's an alternative library pynput that does the same thing as keyboard, but does not require root permissions.

Install with:

pip install pynput

And use with:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

This will immediately cause the keyboard type out "Hello World".

Alex Kaszynski
  • 1,817
  • 2
  • 17
  • 17
-1

$> sudo su

Then enter password for the user and run script file

Saisiva A
  • 595
  • 6
  • 24
-1

For me it worked, after installing all of the module using sudo pip3 install name_of_library and then running my file using sudo python3 name_of_file.py