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
    Commented Nov 11, 2019 at 16:12

2 Answers 2


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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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