#!/usr/bin/env python:找不到命令且权限被拒绝

发布于 2024-11-24 19:14:39 字数 869 浏览 1 评论 0原文

我在文件的顶部有#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆消瘦 2024-12-01 19:14:39

克里斯在问题的评论中提出了很好的建议,彼得的回答涵盖了如果克里斯的建议不起作用并且您在系统上的 sudoer 组中(即,如果您可以修改不属于您的用户的文件)。接下来的内容可能是您的下一个选择。

在命令 shell 中,键入:

which python

此命令将告诉您 python 的工作安装位置。然后你可以相应地修改 python 文件中的 shebang 行。例如,在我的系统上:

$ which python
/usr/bin/python

因此我的shebang将是:

#!/usr/bin/python

请注意,这将使您的文件在您的系统上工作,但很可能会使其无法在其他系统上运行...

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:

which python

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:

$ which python
/usr/bin/python

thus my shebang would be:

#!/usr/bin/python

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...

七禾 2024-12-01 19:14:39

看来最可能的问题是 /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.

素年丶 2024-12-01 19:14:39

像这样的基本 python 代码发生了什么:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "Hello World !"

也许问题出在 GTK 或其他代码上,但不是 #!/usr/bin/env python

What happened with a basic python code like :

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "Hello World !"

Maybe the problem is with GTK or other but not with #!/usr/bin/env python ?

昵称有卵用 2024-12-01 19:14:39

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文