6

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!

tripleee
  • 175,061
  • 34
  • 275
  • 318
Cannon Moyer
  • 99
  • 1
  • 1
  • 5

3 Answers3

6

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.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • This is the way. Surprising that as `root` I am not granted permission to every file in the system, hahaha. – WesternGun Oct 26 '17 at 09:02
  • @FaithReaper> for files, as long as suid/sgid bits aren't set too, execute bit is more of a convenience than an actual access control. In fact, you can run a program with only read permission, by passing it to `ld.so`. For instance on my system: `/lib64/ld-linux-x86-64.so.2 /bin/ls` runs ls, regardless of its execute bit. And at worst if you can read the binary you could always copy it and set execute bit on your copy. So… once you take it as convenience, it follows that this simply lets root use that convenience as well. – spectras Oct 26 '17 at 10:22
1

do like this maybe work:

cd /home/pi/Desktop/control/  
python gpio.py  

Because gpio.py is not a executable file, you should run it by python instead

周伯威
  • 46
  • 1
  • 3
0

In my case, to run sticky (a program for sticky notes in python), on Linux Fedora, I had to chown the executable and the files (.py and .css) linked to it:

sudo chown my_user:my_user /usr/bin/sticky
sudo chown my_user:my_user -R /usr/lib/sticky/
sudo chown my_user:my_user -R /usr/share/sticky/

Otherwise, the received errors were:

python: can't open file '/usr/lib/sticky/sticky.py': [Errno 13] Permission denied

gi.repository.GLib.GError: gtk-css-provider-error-quark: sticky.css:1:0Failed to import: Error opening file /usr/share/sticky/sticky.css: Permission denied

In this way I'm able to run sticky without sudo.

Riccardo Volpe
  • 1,471
  • 1
  • 16
  • 30