question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I am using anaconda on chromebook to be able to use python3.6 on it. My issue is that I am trying to replicate something from the curses module. The thing I am trying to replicate is when I press a key, any key, it reads it back to me immediately, like I press “e”, it shows e. If I then press “o”, it replaces the e with an o.

So this me just guessing to see if it works, but my code rn is:

import keyboard

mans = False
while not mans:
    dwn = keyboard.KEY_DOWN
    dun = keyboard.on_press(dwn)
    print(dun)

and it returns as:

Traceback (most recent call last):
  File "keyboardtest.py", line 6, in <module>
    dun = keyboard.on_press(dwn)
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/__init__.py", line 472, in on_press
    return hook(lambda e: e.event_type == KEY_UP or callback(e), suppress=suppress)
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/__init__.py", line 459, in hook
    append(callback)
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/_generic.py", line 67, in add_handler
    self.start_if_necessary()
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
    self.init()
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/__init__.py", line 194, in init
    _os_keyboard.init()
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/_nixkeyboard.py", line 113, in init
    build_device()
  File "/usr/local/conda3/lib/python3.6/site-packages/keyboard/_nixkeyboard.py", line 109, in build_device
    ensure_root()
  File "/usr/local/conda3/lib/python3.6/site-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.

Note how it says “ImportError: You must be root to use this library on linux.

Now, normally, using a sudo would fix it, but python3 is not recognized as a command in sudo for me. When I try using the python command (without 3 for 3.6) it says that the keyboard module is non existent, even though it works for python3.6.

I will appreciate any help avaible!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
bopprehcommented, Jun 9, 2018

Hi @Yolotroll101

I’m sorry for being so blunt. I support this library on my free time. Every minute spent helping people set up their environment (which is totally unrelated), is a minute that is not spent on improving the library, or helping people whose only hope of support is posting here.

You have a Python script, with dependencies, that you need to run as root. That’s something you can get help for in a lot of different places.

To avoid leaving this on a sour note, a few more workarounds that may work for you:

  • download the library as a zip file and import it as a local script, instead of installing.
  • write a shell script to invoke the Python interpreter over your script, using only absolute paths, and run that with sudo.
  • spend some time doing which python, which python3, sudo which pip3, etc, to get a feel for where things are for your user and the superuser.
2reactions
bopprehcommented, Jun 9, 2018

The library needs root because it operates the same way as a keylogger: intercepting every keystroke, system wide. On Linux, with tools used here (/dev/input files), the kernel restricts this operation only to scripts executed by the superuser. There’s an alternative on the works, to use xlib, but it’s not ready yet.

Therefore you must execute the script as superuser. How to do that depends on your system, and is not a question suitable for an issue on this library.

If you just want to create a command-line application mimicking curses, you can use other tools that don’t require running as superuser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Import error: you must be root - Stack Overflow
I'm trying to use the keyboard library ...
Read more >
ImportError: You must be root to use this library on linux ...
You must use a root terminal ( sudo -i or sudo su will change the user to root ). Navigate to the directory...
Read more >
You must be root to use this library on linux." · Issue #420
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 ...
Read more >
keyboard module; must be root problem - Python Forum
I get 'ImportError: You must be root to use this library on linux.'. I found already that I need to install keyboard using...
Read more >
Import Error: You must be root - Raspberry Pi Forums
Hello all, I'm trying to use the keyboard library but continue to get an import error. I ran the program in windows in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found