在linux中执行python文件时权限被拒绝 [英] Permission Denied when executing python file in linux

查看:161
本文介绍了在linux中执行python文件时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Raspberry Pi 2 B+,并且使用的是 Raspbian.我有一个位于 /home/pi/Desktop/control/gpio.py

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

当我在命令行中输入 /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

我也试过在运行该命令之前运行 sudo -s 但那不起作用.我的 python 脚本正在使用 Rpi.GPIO 库.

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!

推荐答案

您将收到此错误,因为您没有对文件的 execute 权限.有两种解决方法:

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

  1. 一开始就没有执行文件.通过运行 python gpio.py python 将通过读取文件来加载文件,因此您不需要具有执行权限.
  2. 授予自己执行权限.您可以通过运行 chmod u+x yourfile.py 来完成此操作.

  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.

但是,除非您在顶部添加 shebang,否则这样做将不起作用你的python程序.它会让你的 linux 知道它应该启动哪个解释器.例如:

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

这将尝试使用您当前的 $PATH 设置运行 python.如果你知道你想要哪个 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

记住shebang必须是程序的第一行.

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

这篇关于在linux中执行python文件时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆