I am trying to get my Raspberry Pi to open a terminal and run a Python script when it boots up. I have followed the instructions of some guides and have so far been unsuccessful.

My steps

I have made a Python file called testfile.py located in /home/pi/Documents/testfile.py

The contents are as follows:

input('press any key to exit')

I have added a file called testfile.desktop to /home/pi/.config/autostart/testfile.desktop.

The contents are as follows:

[Desktop Entry]
Exec=/usr/bin/python3 /home/pi/Documents/testfile.py

When I double click this file, the terminal opens and the Python script runs as expected, but when I reboot the Raspberry Pi the script doesn't run.

2 Answers 2


I duplicated what you did using an up-to-date Raspbian Buster in a Pi 4 B. (I wish all questions were as clear and complete as yours!)

Double-clicking the file on my machine brought up the thonny IDE. (Note: The 'any' key doesn't work because input() needs an enter to complete.) On reboot, I observed the same result you did. However, installing xterm and changing the Exec line to

Exec=xterm -hold -e '/usr/bin/python3 /home/pi/Documents/testfile.py'

causes an xterm window to open and the Python program to run. I was following these debugging instructions.

So, there is some interaction I don't understand between autostart, Python, and terminal. Perhaps you could try a Python program that leaves some evidence in the file system, such as writing a tiny output file.

Edit: See this answer which appears to be more useful than mine.

  • 1
    That seems to have done it. I was following the same guide you linked, but didn't catch the xterm installation part (should have added that to the original question).
    – Achmed
    Commented Mar 22, 2020 at 16:17
  • 1
    If you discover why it doesn't work without xterm, please let everyone know.
    – Bob Brown
    Commented Mar 22, 2020 at 16:25
  • 2
    Found an alterative way using x-terminal-emulator instead of xterm. (raspberrypi.stackexchange.com/questions/27581/…). still not sure why normal exec doesn't work and i can't find a way of getting to the logs of the startup. Commented Mar 22, 2020 at 16:45

Create a file:

nano /etc/systemd/system/startupbrowser.service  

Put all lines below there:

Description=testfile service

ExecStart=/usr/bin/python /home/pi/Documents/testfile.py



Save the file and reload the daemon:

sudo systemctl daemon-reload

Test that is your code is running:

sudo service testfile start

Check the log by:

journalctl -f -u testfile.service

It will give you the real-time log of your python code to troubleshoot what's going on.

If everything was good as you intended, run this command to enable is as a startup service:

sudo systemctl enable testfile.service
  • 2
    This will run the python script as a sevice, which is interesting, but will it show the terminal and the outputs of the script?
    – Achmed
    Commented Mar 22, 2020 at 16:18
  • 1
    @Achmed Yes, easily. The answer has updated, check it again. Commented Mar 22, 2020 at 22:01
  • ("```lang-none" for the second block?) Commented Mar 23, 2020 at 4:13
  • @PeterMortensen Thank you. I have changed the second part but I think that was not distinguished. Commented Mar 23, 2020 at 12:27

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.