I’m trying to run a pyautogui program in background on a raspberry pi 4

I am using a pyautogui to automate stuff with my raspberry pi 4 and I wanted to run the python program as soon as I boot in my raspberry pi

The code that I used is: sudo nohup python3 main.py &

All my other programs who doesnt have the pyautogui package of python execute properly but this doesn’t!

I know the error is in pyautogui package and I want that package for my project

The error that arises is:

File "./server.py", line 14, in <module>
    import pyautogui as pya
  File "/usr/local/lib/python3.7/dist-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/usr/local/lib/python3.7/dist-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/lib/python3.7/os.py", line 678, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'
INFO:     Stopping reloader process [1006]

If I run the same command that is: sudo nohup python3 main.py & after loggin in and opening a terminal and then entering this command then this works exactly as expected.

I think that the error is probably due to the display not initialised

My python program contains this code

import pyautogui

pyautogui.moveTo(0,100)

And please note that the above program is just for demonstration.. if this works then I am going to implement other awesome stuffs

Source: Python Questions

LEAVE A COMMENT