I have created a .py file that turns on a LED, as in the tutorials on raspberrypi.org.

It is my understanding that I should be able to open the LXTerminal in Raspbian, type the following command and have the .py file execute properly.

sudo python led_on.py

The .py script executes fine in the python terminal so the script is good.

It is my understanding that when I type sudo python led_on.py in the LXTerminal it should also work.

Are my statements correct? Should this command in the LXTerminal work?

sudo python led_on.py

I have edited to provide more information:

pi@raspberrypi:~ $ ls -al
total 120
drwxr-xr-x 19 pi   pi   4096 Apr 18 00:18 .
drwxr-xr-x  3 root root 4096 Apr 15 08:13 ..
-rw-r--r--  1 pi   pi     69 Apr 15 08:55 .asoundrc
-rw-------  1 pi   pi   1899 Apr 18 07:25 .bash_history
-rw-r--r--  1 pi   pi    220 Mar 18 01:09 .bash_logout
-rw-r--r--  1 pi   pi   3512 Mar 18 01:09 .bashrc
drwxr-xr-x 11 pi   pi   4096 Apr 15 08:26 .cache
drwxr-xr-x 15 pi   pi   4096 Apr 15 09:58 .config
drwx------  3 pi   pi   4096 Mar 18 01:58 .dbus
drwxr-xr-x  2 pi   pi   4096 Apr 17 19:50 Desktop
drwxr-xr-x  5 pi   pi   4096 Apr 15 08:13 Documents
drwxr-xr-x  2 pi   pi   4096 Apr 15 22:51 Downloads
-rw-r--r--  1 pi   pi      6 Apr 17 22:52 findme
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 .gstreamer-0.10
-rw-r--r--  1 pi   pi     21 Apr 17 23:56 hello.py
drwxr-xr-x  2 pi   pi   4096 Apr 16 08:27 .idlerc
-rwxr-xr-x  1 pi   pi     48 Apr 17 22:53 led_on.py
drwxr-xr-x  3 pi   pi   4096 Apr 15 08:13 .local
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 Music
-rw-------  1 pi   pi      0 Apr 15 09:57 .mysql_history
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 Pictures
-rw-r--r--  1 pi   pi    675 Mar 18 01:09 .profile
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 Public
drwxr-xr-x  2 pi   pi   4096 Apr 15 08:13 python_games
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 Templates
drwxr-xr-x  3 pi   pi   4096 Apr 15 08:13 .themes
drwx------  4 pi   pi   4096 Apr 15 09:07 .thumbnails
drwxr-xr-x  2 pi   pi   4096 Mar 18 01:58 Videos
-rw-------  1 pi   pi     56 Apr 18 00:18 .Xauthority
-rw-------  1 pi   pi    353 Apr 18 00:18 .xsession-errors
-rw-------  1 pi   pi    353 Apr 17 23:12 .xsession-errors.old
pi@raspberrypi:~ $ sudo python led_on.py
pi@raspberrypi:~ $ 

I'm mainly looking for a yes or no answer. "Does the LXTerminal execute Python scripts correctly assuming all conditions are met correctly?"

If you see something wrong here please let me know.

  • 3
    Could you cut&paste or photograph what you are typing in and the system's response. All we know at the moment is that you think something is wrong.
    – joan
    Commented Apr 18, 2016 at 8:23
  • Can a stone fly "assuming all conditions are met correctly?" Yes, of course it can... if the conditions include tying it to an air plane. (your question is almost entirely hypothetical and you haven't posted the contents of the python script. Most likely, you're having some issue related to missing environment variables or permissions but without more info, it's a guessing game)
    – jDo
    Commented Apr 20, 2016 at 10:46

1 Answer 1


My original answer, before more information was provided, and a yes or no answer requested

If you are in the same directory/folder as the script and try the following, does it work correctly?

sudo python ./led_on.py

that additional ./ says to explicitly look in the folder you are in. Because you've used the sudo command, some things, including where the pi will look for a script with the name you gave it will change, so being more definitive might well solve the issue

The other easy to miss thing I would check is that the script is marked as executable; to check from the terminal you can type:

ls -al

or probably there will be an alias (like a typing shortcut) as you use that one a lot, it's usually automatically set as


to see the permissions, if the last letter (in the last column) in the weird sequence of letters and spaces on the left is an x then it should be executable. Either way, if you're starting off it won't hurt to learn how and rule it out now for any more troubleshooting. If you would type:

sudo chmod +x ./led_on.py

that should mark the script as an executable file

If both of ths things didn't work (don't forget to try the first one again after you've done the second and made the file executable if you haven't already) then you can try using the absolute path to the script. I don't know where the script is saved but if it is in the home directory, in a folder called scripts, which would make sense the command would look like this:

sudo python /home/pi/scripts/led_on.py

that's telling the pi the whole route to the script from the root of the computer at / all the way to your scripts directory so should be a solid route.

The fact that you're being asked to use sudo would be a bit off in your home directory as you shouldn't need to become the root user to execute files you own which makes me think i should double check with you where the script is located. It might just be that you aren't in a security group that can access the GPIO but then that command would be better with some switches to make sure that when you use Sudo you don't end up creating files you don't own in your home directory. There's no point over complicating things if any of the things I've already said resolve the problem but when you've fixed this I'd recommend reading the output of

sudo --help

to get a feel for all that the sudo command does.

I'll add here that the help switch does not give enough information in my opinion and I feel I should have pointed you to the command

man sudo

although i wasn't sure as to the availability of man pages or the man command, simply installing man using apt-get would not have been difficult, also the information is available online on the sudo website so my apologies for not directing you to a more complete source of information

If none of that worked just pop back with a bit more information about where the script is saved and if you could include the content of the script (just copy and paste it) so we can see if there's anything inside that might be the problem, it'll be more likely someone will be able to help.

And don't worry, i've been doing this for years and i've been writing a script for days and making error after error, it's natural when coding, with the right development tools, the errors get picked up but even then, there's usually something that doesn't quite work the first time

As you've changed the nature of the question, I'm going to answer the question properly as it seems you wan to understand what is happening

I'll add detail to my answer as i have already provided the solution but I remember and understand how frustrating Linux can be at first, and how little time people often have to explain complex concepts to people who are simply trying to understand and learn, so that they can build more and better things. I will also try to include some inks to sites which contain tutorials or additional information which would help your understanding but the content of which will not all be applicable to this exact issue and so should not be included here,.

So here goes a deep dive:

Firstly to answer your yes or no question regarding the command shell working properly... Yes.

The bash shell, which is the shell that is run in the raspbian terminal, is one of the most used pieces of software in the world. It is therefore a very well tested system which you are unlikely to find a bug in. The python interpreter could be similarly described. However, the conditions you must meet, as you put it, are not always obvious, especially to a new user, and sometimes doubly so when the system is not a standard desktop or laptop and some settings which are fairly consistent on other debian based systems have had to be changed.

A useful introduction to bash can be found in the learning section of the pimoroni website

Secondly: so you know that you made no error here, the permissions appear to be set correctly:

-rwxr-xr-x  1 pi   pi     48 Apr 17 22:53 led_on.py

This line shows that the user 'pi' can read, write and execute the file and the group 'pi' can read and execute, but not write the file; the x in the final column shows that the file has been marked as executable.

So far so good but it would seem but the command is not working,

The explanation of your issue is probably a lot more complex than you'd like, I thought i had found an easy workaround but the complexity of the underlying system has grown over the past couple of years and I'll freely admit that i have learned a little in researching and rewriting the information in a way that you do not have to understand a lot more of how linux works to understand.

I believe this will be the reason why the examples on the raspberry pi website are all run through IDLE, and why they never specifically ask you to execute a python script outside of that IDE, which has been written in a way as to invisibly hide this complexity for the user.

However though truly understanding the issue in a way which you can integrate into your overall knowledge and experience (or Grokking the concept as it is known) will greatly help you to understand Linux and the security measures that make it such a secure and reliable platform.

The key is the sudo command and how it works.

In the simplest terms, when you run the python interpreter as sudo, you are temporarily not acting as the user 'pi'. Whilst your terminal maintains the appearance of you using it, another invisible terminal is created in the background for the root user, your command is passed to that terminal where it is run by he root user, and the output is returned back to you, where it is output to the screen.

The issue is that when that temporary terminal is created, the root user is not in the directory that you were and the system variables that have been set up for you have different values which allow the root user to do the things required.

The sudo command does have some extra features built in to aid you by reducing the complexity of the commands you will need to input.

One of these features is that if he path to a file has been properly specified, that path is converted to the absolute path and that is used by the root user.

Unfortunately, the command

sudo python led_on.py

does not fully specify the path to the file you are referring to, this might seem ridiculous, but think of the commands that you can use from anywhere on the system such as ls or cd; those programs aren't located in the folder you are in and must have a mechanism to make them work.

This mechanism is a check of a special system variable known as a global which is always available. This global is used to to keep track of all the places it should look when you enter a command and is called $PATH.

As you are able to install and run software in your home directory, your $PATH is set up at login and can be unique for each of the users the system has.

This means that the command you typed is ambiguous, as the file led_on.py could be in any of the places listed in your $PATH. For that reason it cannot be passed to the root user in the invisible terminal.

To test this, you can try two different things

1) Become the root user temporarily (though be careful not to change or delete anything as you will be able to modify or delete anything or in fact, everything, without being asked for a password)

to do this, enter the following command:

sudo su

and then enter your password when prompted. You should see that your location, as well as the username at the start of the command prompt have changed and you are ow the user root, in the root user's home directory. as your file is not here, running

python led_on.py

will have no effect

typing exit should take you out of superuser mode and you should become the user pi again.

2) Copy your script to a location which is in all of the path variables by default

From the directory containing your script, enter the following command:

sudo cp ./led-on.py /usr/bin/led_on

then you should be able to run the following command successfully:

sudo python led_on

as the directory /usr/bin is in he root user's $PATH, the script is found and should execute

note though, i still had to specify the folder to copy from explicitly by adding ./ before he script name.

The pointers ./ and ../ you will see at the top of every directory if you issue the ls -al command. They point to the folder you are currently in, known as the working directory, and it's parent, if you have ever typed cd .. you are changing to the folder which is the parent of the current working directory by using these pointers. cd ../../.. would jump you up 3 parents.

So to loop all the way back round to my original answer, the correct way to issue the sudo command so that it has enough information for the root user to execute the script; is to include he pointer to he current working directory, removing the ambiguity, and thereby allowing the sudo command to pass the full path to the script (which is /home/pi/scripts/led_on.py) to the root user, who can then execute the script, as the root user then knows where the script is located, and has full permissions to the GPIO system. So as I originally asked but now know will only not work if your system is configured incorrectly (or just weirdly), the command should be:

sudo python ./led_on.py

Which was the first thing i suggested, but i hope now makes a lot more sense.

If it does not work, and you read the man page, you will have noticed there are lot of additional switches for the sudo command. One of the following may be more successful:

sudo -H python led_on.py
sudo -E python led_on.py

Depending upon which raspberry pi you have, simply entering the command

python led_on.py

Might work, but it also might not.

The GPIO system changed radically when a system known as (device trees)3 was added to allow for the automatic detection of the different models of Pi and their differing capabilities as well as allow for he autodetection and correct setup of the GPIO pins for HATs when they were introduced.

There is some in depth documentation about the GPIO system available on the GPIO Section of he Low level Hardware wiki page at elinux.org

As is warned frequently, directly using the GPIO in the manner you describe is not really recommended beyond flashing an led on and off, until you have plenty of experience or stick more closely to the tutorials, to offset this risk of destroying your pi, an addon such as Pimoroni's explorer hat, the Gertboard, or some more specific board such as the Adafruit Motor/Servo hat could be more appropriate. for your continued experiments.

If you wish to continue directly interacting, I Couldn't reasonably blame you as I'm naturally the kind of person who will tinker until i burn something out before buying the safety feature addon with my second board. So with that in mind, it seems as though he easiest way to interact directly with the GPIO pins is by using Wiring Pi, although i remember reading that this may be quite out of date so proceed at your own risk.

Lastly, (although i may add more links for others who may find this answer if i come across updated or particularly easy to understand ones) I'm assuming that you wish to use your pi for more than just a mechanism by which to control or interact with electronics.

If that assumption is incorrect, a device such as an Arduino might better suit your needs and whilst im fully of the belief that the foundation should be supported by buying some official boards, the hardware is open source and so clone boards can be found very cheaply ($5 or less) online for you to try before buying a more expensive (but honestly much better made) official boards

  • Thank you for your help you did give me a couple more things to look at. But as with the added information i added to the question i'm looking for a yes or no answer first and if the answer is yes then ill continue this thread.
    – user45135
    Commented Apr 18, 2016 at 14:42
  • As i said in my edited answer, the answer to your yes or no question is Yes "Does the LXTerminal execute Python scripts correctly assuming all conditions are met correctly?" - YES Commented Apr 25, 2016 at 11:54
  • I'll add a bit more by pointing you to the pigpio library here which shhould let you get full control of the gpio pins without sudo Commented May 14, 2016 at 9:16

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.