Search code examples
pythonkeyboardpynput

python pynput ERROR: failed to acquire X connection: Bad display name ""', DisplayNameError('')


Traceback (most recent call last):
  File "/boot/system/key.py", line 1, in <module>
    from pynput.keyboard import Key, Listener, Controller
  File "/home/user/.local/lib/python3.8/site-packages/pynput/__init__.py", line 40, in <module>
    from . import keyboard
  File "/home/user/.local/lib/python3.8/site-packages/pynput/keyboard/__init__.py", line 31, in <module>
    backend = backend(__name__)
  File "/home/user/.local/lib/python3.8/site-packages/pynput/_util/__init__.py", line 76, in backend
    raise ImportError('this platform is not supported: {}'.format(
ImportError: this platform is not supported: ('failed to acquire X connection: Bad display name ""', DisplayNameError(''))

Try one of the following resolutions:

 * Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly

i what to RUN my python script but i get this Error when i trying to run using root user or sudo


Solution

  • if anyone gets this error so I get answer:

    use this command in the terminal: if you have this

    echo $DISPLAY :1

    then do this or visa versa

    DISPLAY=":0" python3 filename.py