#!/usr/bin/env python:找不到命令且权限被拒绝
我在文件的顶部有#!/usr/bin/env python
。那么当我运行该文件时它应该起作用吗?但事实并非如此。当我使用 python file.py
tho
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class App1:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
app = App1()
app.main()
Python 时它可以工作,这就是发生的事情:
jiewmeng@jiewmeng:/labs/projects/learnPython$ app1.py
app1.py: command not found
jiewmeng@jiewmeng:/labs/projects/learnPython$ ./app1.py
bash: ./app1.py: Permission denied
jiewmeng@jiewmeng:/labs/projects/learnPython$ ll
total 12
drwxr-xr-x 2 jiewmeng jiewmeng 4096 2011-07-16 22:08 ./
drwxr-xr-x 4 jiewmeng jiewmeng 4096 2011-07-16 21:45 ../
-rwxrwxr-x 1 jiewmeng jiewmeng 256 2011-07-16 22:05 app1.py*
I have at the top of the file #!/usr/bin/env python
. So it should work when I run the file? But it doesnt. It works when I use python file.py
tho
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class App1:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
app = App1()
app.main()
Python Here is what happened:
jiewmeng@jiewmeng:/labs/projects/learnPython$ app1.py
app1.py: command not found
jiewmeng@jiewmeng:/labs/projects/learnPython$ ./app1.py
bash: ./app1.py: Permission denied
jiewmeng@jiewmeng:/labs/projects/learnPython$ ll
total 12
drwxr-xr-x 2 jiewmeng jiewmeng 4096 2011-07-16 22:08 ./
drwxr-xr-x 4 jiewmeng jiewmeng 4096 2011-07-16 21:45 ../
-rwxrwxr-x 1 jiewmeng jiewmeng 256 2011-07-16 22:05 app1.py*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
克里斯在问题的评论中提出了很好的建议,彼得的回答涵盖了如果克里斯的建议不起作用并且您在系统上的 sudoer 组中(即,如果您可以修改不属于您的用户的文件)。接下来的内容可能是您的下一个选择。
在命令 shell 中,键入:
此命令将告诉您 python 的工作安装位置。然后你可以相应地修改 python 文件中的 shebang 行。例如,在我的系统上:
因此我的shebang将是:
请注意,这将使您的文件在您的系统上工作,但很可能会使其无法在其他系统上运行...
Chris makes an excellent suggestion in the comments to the question, and Peter's answer covers what you should do if Chris' suggestion doesn't work and you are in the sudoer group on your system (i.e. if you can modify files other than those belonging to your user). What follows could be your next option.
In a command shell, type:
this command will tell you where your working installation of python is located. Then you can modify the shebang line in your python file accordingly. For example, on my system:
thus my shebang would be:
Be aware that this will make your file work on your system but it's highly probable it will make it not runnable on other ones...
看来最可能的问题是 /usr/bin/env 无法由您执行。尝试 sudo chmod 755 /usr/bin/env 。这绝对不是正常状态,因此一定有人手动取消了 /usr/bin/env 上的权限。
It seems the most likely problem is /usr/bin/env is not executable by you. Try
sudo chmod 755 /usr/bin/env
. Definitely this is not the normal state so someone must have manually borked up the permissions on /usr/bin/env.像这样的基本 python 代码发生了什么:
也许问题出在 GTK 或其他代码上,但不是
#!/usr/bin/env python
?What happened with a basic python code like :
Maybe the problem is with GTK or other but not with
#!/usr/bin/env python
?在 Windows 10 中:开始按钮 -> “管理应用程序执行别名”->全部关闭
错误原因:当您在cmd中输入“python”时,它会打开microsoft store。
In Windows 10: Start button -> "manage app execution aliases" -> turn all off
Error reason: when you type "python" in cmd, it opens microsoft store.