Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission Denied when executing python file in linux

I am working with my Raspberry Pi 2 B+ and I am using Raspbian. I have a python script located at /home/pi/Desktop/control/gpio.py

When I type /home/pi/Desktop/control/gpio.py into the command line, I get the message

bash: /home/pi/Desktop/control/gpio.py Permission denied

I have tried running sudo -s before running that command also but that doesnt work. My python script is using the Rpi.GPIO library.

If someone could please explain why I am getting this error it would be appreciated!

like image 482
Cannon Moyer Avatar asked Oct 02 '15 02:10

Cannon Moyer


1 Answers

You will get this error because you do not have the execute permission on your file. There are two ways to solve it:

  1. Not executing the file in the first place. By running python gpio.py python will load the file by reading it, so you don't need to have execute permission.
  2. Granting yourself execute permission. You do this by running chmod u+x yourfile.py.

    However, doing so will not work unless you add a shebang at the top of your python program. It will let your linux know which interpreter it should start. For instance:

    #!/usr/bin/env python
    

    This would try to run python using your current $PATH settings. If you know which python you want, put it here instead.

    #!/usr/bin/python3
    

    Remember the shebang must be the very first line of your program.

like image 182
spectras Avatar answered Sep 30 '22 16:09

spectras