so i want to autorun my python script after boot in a CLI boot however it seems that its giving me an error "Cannot connect to Xserver". My python script uses a camera i used crontab to autorun my python script.

  • How do you start the python script? – Ingo Nov 11 '19 at 16:12

If the GUI is already configured to run, you just need to set the DISPLAY environment variable in the script that launches your Python script.

If you're using /etc/rc.local just add export DISPLAY=:0. If you're using a .service file add Environment=DISPLAY=:0 to the [Service] section (see Raspberry Pi run commands on boot).

It might be helpful to make sure your script is starting as the same user as the GUI, in which case start python using sudo -u pi python.

If your GUI isn't starting, you may need to enable it or install it first. For the former, run sudo raspi-config and navigate through the "Boot Options" menu to the "Desktop / CLI" option, then select "Desktop" (see here). For the latter, use the tasksel command and select Desktop Environment (How can I enable the GUI on the Debian "Squeeze" Image?).

| improve this answer | |

If you boot to the command line the OS quite correctly reports "Cannot connect to Xserver" because there isn't one!

If your (unlisted) code needs X either boot to GUI or run startx

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.