简体   繁体   中英

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!

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM