'Using pyautogui in Unix

I have a working test scripts that perform some UI Action in headless mode.

I am trying to get it working on Unix Server. I got the below error to start with:

    import pyautogui
../../Libs/pyautogui/__init__.py:540: in <module>
    from . import _pyautogui_x11 as platformModule
../../Libs/pyautogui/_pyautogui_x11.py:161: in <module>
    _display = Display(os.environ['DISPLAY'])
../../../../anaconda3/lib/python3.9/os.py:679: in __getitem__
    raise KeyError(key) from None
E   KeyError: 'DISPLAY'

Looking at few suggestions, I tried export DISPLAY=:0 , export DISPLAY=localhost:0.0, etc But keep getting the below error:

ImportError while loading conftest '/home/sengutr/E2E_Automation_Framework/e2e-automation-framework/TestCases/conftest.py'.
../conftest.py:3: in <module>
    import pyautogui
../../Libs/pyautogui/__init__.py:540: in <module>
    from . import _pyautogui_x11 as platformModule
../../Libs/pyautogui/_pyautogui_x11.py:161: in <module>
    _display = Display(os.environ['DISPLAY'])
../../Libs/Xlib/display.py:89: in __init__
    self.display = _BaseDisplay(display)
../../Libs/Xlib/display.py:71: in __init__
    protocol_display.Display.__init__(self, *args, **keys)
../../Libs/Xlib/protocol/display.py:89: in __init__
    self.socket = connect.get_socket(name, protocol, host, displayno)
../../Libs/Xlib/support/connect.py:87: in get_socket
    return mod.get_socket(dname, protocol, host, dno)
../../Libs/Xlib/support/unix_connect.py:127: in get_socket
    raise error.DisplayConnectionError(dname, str(val))
E   Xlib.error.DisplayConnectionError: Can't connect to display "localhost:0.0": [Errno 111] Connection refused

Cant make out what this means, any idea?



Solution 1:[1]

You can try putting this in your terminal:

export DISPLAY="$(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RiveN