6

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ammu
  • 61
  • 1
  • 1
  • 2
  • 3
    Can you do a `ls -l filename.py` and post what it says .. I'm curious about the file permission (though `chmod +x filename.py` would be the right way to make the file executable). – Levon Jun 16 '12 at 03:45
  • Do you have `#!/usr/bin/env python` in the first line of your script? – Pavel Strakhov Jun 16 '12 at 03:51
  • Re `#!/usr/bin/env python` vs `#!/usr/bin/python` I was severely admonished by some person on SO for using the former (which all of my own scripts still do) because presumably you could not be sure which Python would execute in case you had more than one installed, so the idea was to always specify the full path for the one you wanted). Maybe I should make this a question and post it on SO. – Levon Jun 16 '12 at 03:57
  • Using `/usr/bin/env` is fine. It is simply a way to say "use what you find on my `PATH`" which is perfectly fine if your `PATH` is constructed sanely. I would say it is preferred if you're going to distribute your scripts, because then they will be able to use the end-user's `PATH`. But if you have a need to explicitly use one specific Python binary, then yes, by all means, specify that. – Mattie Jun 16 '12 at 11:45

3 Answers3

8

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'
Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    OP is asking about a Python script, so the shebang should be: `#!/usr/bin/env python`. And OP does say that exactly that way of running it is what gives the error. – lvc Jun 16 '12 at 03:48
  • @ivc you are right .. I'll update my answer - thanks for catching that. – Levon Jun 16 '12 at 03:50
  • Really thanks a lot every one.. @Levon Thanks for editing my question + #!/usr/bin/python + print "Hello, World!" Here above is my program. – Ammu Jun 16 '12 at 04:11
  • @Ammu You are welcome. Can you give us the output of the `ls -l filename.py` command so we can see the file permissions? – Levon Jun 16 '12 at 04:14
  • Hi,Levon terminal is displaying following message 1. -rw-rw-r-- 1 owner owner 40 Jun 15 15:45 first.py – Ammu Jun 16 '12 at 04:59
5

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.

Mattie
  • 20,280
  • 7
  • 36
  • 54
  • Hi Zigg, 1.Thanks a lot for your answer 2.Now Its working, But what is the problem behind chmod -x and chmod u+rx? – Ammu Jun 16 '12 at 04:19
  • `chmod -x` will remove execute permission from user, group, and other, but again only if the umask doesn't include those execute bits—see the chmod manual page at http://linux.die.net/man/1/chmod for more info. `chmod u+rx` will set the user read and execute bits regardless of umask. – Mattie Jun 16 '12 at 04:34
4

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.

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44