bash permission denied for python

2019-02-18 11:18发布

问题:

I am new to Ubuntu... I am trying to run my first simple python program "Hello World" ... After running following commands in terminal

1. chmod +x filename.py 
2. ./filename.py

terminal is showing following error "bash: ./filename.py: Permission denied" what can I do for solve about problem?

回答1:

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'


回答2:

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.



回答3:

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.