keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

" raise ImportError('You must be root to use this library on linux.') ImportError: You must be root to use this library on linux."

Open lotosprogrammer opened this issue 4 years ago • 10 comments

Yea andI cant find solution online. Its only when I try to get input so yea keyboard module is the problem. any fix? entire error log

lotosprogrammer avatar Jan 27 '21 09:01 lotosprogrammer

check "Known limitations" in the READMED.md "To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requires root." This is intentional. The work-around is to run your program with root permissions.

farmerpat avatar Jan 30 '21 17:01 farmerpat

The real work around is that you can add the user running the program to the input group. (at least for a similar python module that uses /dev/input

You can add the a user via sudo usermod -a -G <group> <username> there are security concerns to be made before doing this. Adding your user like this means that any program you run as the user has access to create any kind of input in /dev/input

sebastiansam55 avatar Jan 30 '21 19:01 sebastiansam55

@sebastiansam55 apparently not on my system:

patrick@ubuntu:~/hacks/editor/python$ groups patrick
patrick : patrick sudo input
patrick@ubuntu:~/hacks/editor/python$ ./keyboard_listener.py 
Traceback (most recent call last):
  File "./keyboard_listener.py", line 47, in <module>
    keyboard.on_press(key_press)
  File "/usr/local/lib/python2.7/dist-packages/keyboard/__init__.py", line 474, in on_press
    return hook(lambda e: e.event_type == KEY_UP or callback(e), suppress=suppress)
  File "/usr/local/lib/python2.7/dist-packages/keyboard/__init__.py", line 461, in hook
    append(callback)
  File "/usr/local/lib/python2.7/dist-packages/keyboard/_generic.py", line 67, in add_handler
    self.start_if_necessary()
  File "/usr/local/lib/python2.7/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
    self.init()
  File "/usr/local/lib/python2.7/dist-packages/keyboard/__init__.py", line 196, in init
    _os_keyboard.init()
  File "/usr/local/lib/python2.7/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
    build_device()
  File "/usr/local/lib/python2.7/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
    ensure_root()
  File "/usr/local/lib/python2.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.

farmerpat avatar Jan 30 '21 21:01 farmerpat

Did you reboot? I've also had to chmod 660 /dev/[u]input it's very inconsistent as you have found out however

sebastiansam55 avatar Jan 30 '21 22:01 sebastiansam55

Given that ensure_root() is this:

def ensure_root():
    if os.geteuid() != 0:
        raise ImportError('You must be root to use this library on linux.')

unless you edit that file as well, I don't think just adding your user to the right group will be enough.

seanbeaton avatar Mar 30 '21 10:03 seanbeaton

I'm using Spyder running through Anaconda, I tired to install Keyboard via sudo pip install but I'm still getting the error that

ImportError: You must be root to use this library on linux.

using sudo /home/***/anaconda3/bin/python -m pip install keyboard

also didn't solve my problem.

which python
/home/***/anaconda3/bin/python

which pip
/home/***/anaconda3/bin/pip

sudo which pip
/usr/bin/pip

how can I give the root access to this library?

AminDar avatar Mar 24 '22 15:03 AminDar

@AminDar The issue in on runtime, not on install.
sudo python3 your-app.py

However this will cause issues with graphical apps that use X as the root user won't have access to the X Server.

Avasam avatar Jul 19 '22 23:07 Avasam

when I use sudo python3 your-app.py, I can only use the python in the system, but I can't use the python from anaconda.

ArchibaldChain avatar Dec 23 '22 12:12 ArchibaldChain

@ArchibaldChain I'd recommend not starting the app as root, and instead install keyboard from master and setup group rules for your user:

pip install git+https://github.com/boppreh/keyboard.git#egg=keyboard
sudo usermod -a -G tty,input $USER
sudo chmod +0666 /dev/uinput
echo 'KERNEL=="uinput", TAG+="uaccess""' > /etc/udev/rules.d/50-uinput.rules
echo 'SUBSYSTEM=="input", MODE="0666" GROUP="plugdev"' > /etc/udev/rules.d/12-input.rules
echo 'SUBSYSTEM=="misc", MODE="0666" GROUP="plugdev"' >> /etc/udev/rules.d/12-input.rules
echo 'SUBSYSTEM=="tty", MODE="0666" GROUP="plugdev"' >> /etc/udev/rules.d/12-input.rules
loginctl terminate-user $USER

As others mention in this thread, you can also run with sudo, however that's not always an option. In my case, I need to access the X11 server to record the screen. And even root can't access another user's X11 server.

Avasam avatar Dec 23 '22 16:12 Avasam

I didn't want to edit any permissions, so here is a workaround that allowed me to run my python app in sudo mode without having to install my modules globally:

  • Create a venv for your python project
    • python3 -m venv env
  • Install your modules as you would normally via pip
    • pip3 install keyboard
  • Copy the modules you installed via pip from venv/lib/python3.xx/site-packages/ directory into your project's root directory
  • deactivate and delete the env folder
    • deactivate
  • run with sudo: sudo python3 my_python_app.py

romjab avatar Nov 15 '23 13:11 romjab