I have a script which uses the arrows on the keyboard to provide user input. The script required the keyboard package to be installed with pip install keyboard.

The script works fine outside the virtual environment if I run it with sudo.

I would like to be able to run that script inside the virtual environment without giving root access to that environment.

Is there a way to allow the virtual env to use the keyboard without giving it root privileges?

PS my keyboard is connected to the Pi via bluetooth, not sure if that makes a difference.

  • Read this answer to see if it applies. virtualenv without sudo
    – Sanders
    Dec 25, 2021 at 18:44
  • you don't need to give full root access to something. such measures are often handled by granting full access to something else that you then trust to take actions on behalf of users, limiting itself to only authorized actions. the keenel has the ability to mess with hardware, but we trust it to a degree not to except in certain situations. Key is figuring out exactly what permissions your script user needs, and limiting it to those.
    – Abel
    Dec 25, 2021 at 20:45

1 Answer 1


According to documentation this is to be expected:

To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requires root.

Adding the user that runs the Python script to the relevant group should be enough. Depending on the OS, the group name will be input, plugdev or something similar. Check the ownership of /dev/input/* files to find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.