Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I think I am misunderstanding something here. I have made an easy python testing file to see how permissions affect the use of python files. I did so in order to be able to answer 64bit ubuntu 12.04 python cannot run an existing python file

SetUp

I have made a test.py file with the contents

print 'I am working'

Test case 1

ls -al test.py 
-rw-r--r-- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
I am working
  • How come python is executing this file even though I did not do chmod +x test.py?

Test case 2

chmod 400 test.py
ls -al test.py 
-r-------- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
I am working

So apparently python only needs read permission in order to execute my file?

Test case 3

chmod 200 test.py
ls -al test.py 
--w------- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
python: can't open file 'testo.py': [Errno 13] Permission denied

Write permissions are insufficient (and for the record, only executable permissions are insufficient as well).

  • How come python executes files without executable permissions?
share|improve this question

2 Answers 2

up vote 5 down vote accepted

Yes, Python only requires the file contents to be read. Recall that Python is an interpreted language (like PHP, Ruby, etc.) and just processes the contents of that file, rather than executing it; python is the executable here!

For proper background information; note that you can run scripts two ways:

  • Calling the interpreter with the file as input/argument does not require other than read permissions, e.g.:

    python myscript.py
    
  • Run the script by its shebang does require the executable bit set.

    ./myscript.py
    

    The shebang (first line in the file) is then something like

    #!/usr/bin/env python
    
share|improve this answer

"python only needs read permission" to read the content of your file and process the code.

your user can execute python. then python can read file (because of 400). If you want to execute file directly like " ./testo.py " then you need a execute permissions of your file.

share|improve this answer
    
I have noticed that in my question. Can you give a source and an explanation why that is the case (or a short summary of the source)? –  don.joey Dec 24 '13 at 10:34
    
your user can execute python. then python can read file (because of 400). If you want to execute file directly like " ./testo.py " then you need a execute permissions of your file. –  Dian Nedelchev Dec 24 '13 at 10:37
    
Makes sense. Can you add it to your answer? –  don.joey Dec 24 '13 at 10:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.