Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.8K
Visualizações
Import error: you must be root

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.
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda