0

Im trying to figure out how to use the keyboard library. Every time I try to run it on spyder, I get a ImportError: You must be root to use this library on linux. I tried countering this by running the sudo python3 command in a terminal with the specified program below. However, when I do, I get a different error as seen below:

Traceback (most recent call last):
  File "Mouse_simulator.py", line 9, in <module>
    import pyautogui
  File "/usr/local/lib/python3.8/dist-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/usr/local/lib/python3.8/dist-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 80, in __init__
    self.display = _BaseDisplay(display)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 62, in __init__
    display.Display.__init__(*(self, ) + args, **keys)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/protocol/display.py", line 129, in __init__
    raise error.DisplayConnectionError(self.display_name, r.reason)
Xlib.error.DisplayConnectionError: Can't connect to display ":0": b'No protocol specified\n'

After reading similar posts, I think it has something to do with the program requiring root access to get the correct display from a specific file, but I'm not sure. I cannot find a specific cause after trying numerous options.

The program I'm testing is below:

import pyautogui
import time
from keyboard import is_pressed as press

distance = 200
#time.sleep(5)

while distance > 0:
    """draw a spiral rectangle"""
    
    if press("q"):
        #if q is pressed, then break
        break
    
    pyautogui.drag(distance, 0, duration=0.5)   # move right
    distance -= 5
    pyautogui.drag(0, distance, duration=0.5)   # move down
    pyautogui.drag(-distance, 0, duration=0.5)  # move left
    distance -= 5
    pyautogui.drag(0, -distance, duration=0.5)  # move up
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
  • Have you tried using commands to make the file run as root? `sudo chown root:root /usr/bin/speech-test.py` and `sudo chmod 4755 /usr/bin/speech-test.py` – Chris Jun 20 '22 at 03:08
  • Does this answer your question? [Running python script as root](https://stackoverflow.com/questions/22574201/running-python-script-as-root) – Chris Jun 20 '22 at 03:09

0 Answers0