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?

4
  • 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). Commented Jun 16, 2012 at 3:45
  • Do you have #!/usr/bin/env python in the first line of your script? Commented Jun 16, 2012 at 3: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. Commented Jun 16, 2012 at 3: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. Commented Jun 16, 2012 at 11:45

3 Answers 3

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'
Sign up to request clarification or add additional context in comments.

5 Comments

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.
@ivc you are right .. I'll update my answer - thanks for catching that.
Really thanks a lot every one.. @Levon Thanks for editing my question + #!/usr/bin/python + print "Hello, World!" Here above is my program.
@Ammu You are welcome. Can you give us the output of the ls -l filename.py command so we can see the file permissions?
Hi,Levon terminal is displaying following message 1. -rw-rw-r-- 1 owner owner 40 Jun 15 15:45 first.py
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.

2 Comments

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?
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 linux.die.net/man/1/chmod for more info. chmod u+rx will set the user read and execute bits regardless of umask.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.