Permission denied for Python script using Bash?

给你一囗甜甜゛ 提交于 2019-12-01 05:17:18

Do you have the appropriate incantation at the top of your python file? e.g.,

#!/usr/bin/python (or alternatively #!/usr/bin/env python)

Just to clarify, chmod +x only makes a file executable, it doesn't run it.

And I'm assuming your script looks like nothing more complex than this:

#!/usr/bin/env python
print 'hello world'

Some possibilities:

  1. What does it say if you type umask? chmod +x will only make a file executable for you if your umask doesn't block the user executable bit. A typical umask such as 0022 will not block the user execute bit, but a umask like 0122 can. (See the Description section of chmod(1) for more info.)

  2. To execute a script such as a Python script, you also need read permission. Try chmod u+rx filename.py and execute the script again.

  3. It's also remotely possible that whatever interpreter you've specified in the file with the "hashbang" line at the beginning of your file (e.g. #!/usr/bin/env python) isn't executable, although that in my experience yields a different error message.

I deal with the same problem on my new system.

It is the third time I tried to solve this, and your post is the first one appearing on google results. My post is late, but think that it will help another users with the same problem.

In my case, it was about partition table setup.

Check in your /etc/mtab file how python script is being stored. Check if there is a clause: noexec

noexec is a flag that forbid executing under the partition. By default, it is set with exec. But, sometimes, this kind of things happen.

Now, it is working fine here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!