moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

How to run script with the environment variables? #452

Closed Apacelus closed 2 years ago

Apacelus commented 2 years ago

I am really sorry I have to ask this, but how do I force the script to use uinput? I am trying to run the script on a headless machine with no xserver.
When I enter this command: sudo python3 logger.py $PYNPUT_BACKEND_KEYBOARD=uinput I get the same error as when running without root privileges and the environment variable:

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...

What am I doing wrong?

moses-palmer commented 2 years ago

Thank you for your report.

PYNPUT_BACKEND_KEYBOARD=uinput will override the backend for the keyboard module, but the mouse module will still use the default modules, and the checks for both are always run when importing pynput.

Use PYNPUT_BACKEND_MOUSE=dummy to force the dummy backend for mouse.

Your code snippet also looks a bit strange; if you run your script with sudo, please ensure that the environment variables are actually passed: sudo env PYSTRAY_BACKEND_KEYBOARD=uinput PYSTRAY_BACKEND_MOUSE=dummy python3 logger.py.