Error When Importing PyAutoGUI in Archlinux

I’m having issues importing pyautogui after installing it.

Output of hostnamectl:

 Static hostname: archlinux
       Icon name: computer-laptop
         Chassis: laptop 💻
      Machine ID: feee6bbdd3b94cdd9de25a09801e7fb8
         Boot ID: 463321eb08054209ad65b5b793c8727f
Operating System: Arch Linux                      
          Kernel: Linux 6.11.5-arch1-1
    Architecture: x86-64
 Hardware Vendor: HP
  Hardware Model: HP Laptop 15-bs2xx
Firmware Version: F.40
   Firmware Date: Wed 2018-05-16
    Firmware Age: 6y 5month 1w 5d  

Here is the error I’m getting when trying to import the pyautogui module:

>>> import pyautogui
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/curtis/Documents/python_projects/test/venv/lib/python3.12/site-packages/pyautogui/__init__.py", line 246, in <module>
    import mouseinfo
  File "/home/curtis/Documents/python_projects/test/venv/lib/python3.12/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/curtis/Documents/python_projects/test/venv/lib/python3.12/site-packages/Xlib/display.py", line 80, in __init__
    self.display = _BaseDisplay(display)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/curtis/Documents/python_projects/test/venv/lib/python3.12/site-packages/Xlib/display.py", line 62, in __init__
    display.Display.__init__(*(self, ) + args, **keys)
  File "/home/curtis/Documents/python_projects/test/venv/lib/python3.12/site-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'Authorization required, but no authorization protocol specified\n'

I’m unsure on where to start with this error. So any direction would be greatly appreciated.

The error you’re seeing when trying to import pyautogui on Arch Linux is likely due to the lack of permission or configuration for accessing the display environment variable ($DISPLAY). pyautogui relies on the Xlib library and mouseinfo to interact with the display, and the error you’re seeing (Authorization required, but no authorization protocol specified) indicates that the session is either missing the authorization to connect to :0, or the required environment variables aren’t properly set in the virtual environment.

Here are some steps to troubleshoot and resolve this issue:

1. Ensure You’re Running in a Graphical Environment

pyautogui and its dependencies need access to the display, so if you’re running this in a headless environment (like over SSH or in a terminal without a graphical session), the import will fail.

To check if you’re in a graphical session, run:

echo $DISPLAY

If this doesn’t output :0 (or another display identifier), it could mean that you’re not in a graphical environment, or the DISPLAY variable isn’t set correctly. If this is the case, try running the script directly from a terminal emulator within your desktop environment.

2. Set Up X11 Permissions

The error message also indicates that authorization is required to connect to the display. You can grant this by running:

xhost +SI:localuser:$(whoami)

This command will allow your user to access the display. Note that xhost allows network connections to the display server and should be used cautiously.

3. Install and Configure Dependencies

Ensure that you have all dependencies for pyautogui properly installed. Run the following to make sure Xlib is up-to-date and that you have an X server running:

sudo pacman -S python-pyautogui python-xlib

Also, consider installing scrot for taking screenshots, as it is a dependency for some pyautogui features on Linux:

sudo pacman -S scrot

4. Verify Authorization Protocols for X11

If the above solutions don’t work, check your X11 authorization. Sometimes, removing stale Xauthority files and restarting the display server can help:

rm ~/.Xauthority

Then restart your graphical session and try importing pyautogui again in your Python environment.

5. Run with sudo if Required

If you’re still facing the issue, try running the Python script with sudo. This should only be a temporary solution for testing, as running scripts with elevated privileges can pose security risks:

sudo python3 your_script.py

However, if this works, it’s likely a permissions issue with the display session.

After testing these steps, try running import pyautogui again to confirm if the issue is resolved. Let me know if you’re still encountering issues!