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

- 4,552
- 18
- 29
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

- 59,890
- 31
- 101
- 209