How To Autorun A Python Script On Raspberry Pi Boot


There are lots of techniques for running a script when the Pi boots and which one you choose will depend on exactly what the script does and what you expect. In this post I’ll explain a technique where the Pi automatically logins as the Pi user and immediately executes a Python script.

This has one major advantage over another popular method (see Running A Python Script At Boot Using Cron) in that because the terminal is up and running text output from the script is visible before you are returned to a usable command line prompt.

Auto Login Setup (optional)

The first step is to enable the Pi to login automatically without requiring any user intervention. This step is optional.

At the command prompt or in a terminal window type :

sudo nano /etc/inittab

followed by Enter. Find the line :

1:2345:respawn:/sbin/getty 115200 tty1

and add a # character to the beginning of the line to disable it so it looks like :

#1:2345:respawn:/sbin/getty 115200 tty1

Under the line add the following :

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

where “pi” is the username.

Type “Ctrl+X” to exit, then “Y” to save followed by “Enter” twice.

Prepare Script

My test script is called “” and is located in /home/pi/. This is what it contains :

print "******************************************************"
print "* This is a test script. There are many like it,     *"
print "* but this one is mine. My script is my best friend. *"
print "* It is my life. I must master it as I must master   *"
print "* my life.                                           *"
print "******************************************************"

You can download this directly to your Pi by using the following command :


Auto-run Script Setup

Now we need to tell the operating system to run the script for the Pi user. In the command prompt or in a terminal window type :

sudo nano /etc/profile

Scroll to the bottom and add the following line :

sudo python /home/pi/

where “/home/pi/” is the path to your script.

Type “Ctrl+X” to exit, then “Y” to save followed by “Enter” twice.

Reboot and Test

To test if this has worked reboot your Pi using :

sudo reboot

When it starts up your script will run and you will see something like this :

Autorun script result

Due to the technique we’ve used the script is run whenever the Pi user logs in. This means if you create other terminal sessions (via SSH for example) the script will run each time.


If it doesn’t work here are some things to try :

  • Run your script manually and check it works correctly
  • Use my example script and check that works
  • Double check the initial steps


    • Hi, I tried the method you’ve described above, however I use a “while True:” loop in my program. My Pi is stuck a black screen during the boot, Is this because the script never completes? Please help resurrect my pi

  1. Thanks Matt great tutorial.

    To the others that get stuck with the black screen this is how I reverted back to how it was.

    When the pi boots up and the script starts running just Ctrl+c which takes you back to the command line, you can # out the added lines in the nano files and delete the # so the file reverts back to how it was.

  2. Thank you so much for this solution. We were trying for 3-4 days to run Python script at boot time but that all went into vain. This solution worked out. many thanx.

  3. Thanks for the guide, my script ran perfectly at startup!

    I am having the same problem of the black screen at boot (probably due to my while true loop). I keep trying to control-C on my keyboard but that doesn’t work. My Mac also refuses to connect to the pi via SSH. Anything else I can try? Otherwise I will just reinstall the os, no big deal.

    • This technique is only suitable for scripts that perform a task and then exit. If they get stuck in a loop they may stop the boot sequence completing.

  4. For those who are facing black screen problem
    Just boot up the pie
    Allow your script to run at same time press ctrl+alt+f1 and then ctrl+c
    You will be prompted with command prompt where you can make changes

Leave A Reply