Autostart python program

Active today
Viewed 63 times

I am trying to get my 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 pythonfile 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]

When i double click this file, the terminal opens and the python script runs as expected, but when i reboot the raspberry the script doesn’t run.

 New contributor

2 Answers


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.


  • 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 11 hours ago
  • If you discover why it doesn’t work without xterm, please let everyone know. – Bob Brown 11 hours ago
  • 1
    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. – en_lorithai 11 hours ago

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 11 hours ago
  • 1
    @Achmed Yes, easily. The answer has updated, check it again. – M. Rostami 5 hours ago

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: