How come a python file is executable even though its permissions are 644?
Solution 1
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, because it would start a new process, the Python interpreter.
./myscript.py
The shebang (first line in the file) should then be something like
#!/usr/bin/env python
to define the interpreter for this file.
N.B. Such a shebang line is also a comment in Python when run in the former form (and thus ignored), so it would work both ways. Could be useful for users in case a different interpreter version is desired for a single run, e.g. python3.10 myscript.py
if you don't like the default python
. That's why you'll probably see the latter form to be fairly common in any entrypoint/script.
Solution 2
"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.
Related videos on Youtube
don.joey
Before I was called Private, but due to namespace polution I am henceforth known as don.joey! For my real avatar (.gif): check here.
Updated on September 18, 2022Comments
-
don.joey over 1 year
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?
-
Eliah Kagan over 4 yearsRelated: Is there any way to stop a user from creating executables and running them? Note that even most executable binaries can be run this way (for example:
/lib64/ld-linux-x86-64.so.2 ./my-binary
).
- How come python is executing this file even though I did not do
-
don.joey over 10 yearsI 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)?
-
Dian Nedelchev over 10 yearsyour 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.
-
don.joey over 10 yearsMakes sense. Can you add it to your answer?
-
ado sar about 2 yearsWhy in the second case the file needs to have executable permissions? Isn't the interpreter also involved which has executable permissions?
-
gertvdijk about 2 years@adosar Well, you won't run the the
python
interpreter (or anything else in the shebang line) at all, so it won't be involved then. (Not sure I understand your question.) -
ado sar about 2 yearsWhen we execute the script via
./myscript.py
the executable permission is need because the interpreter is called from the file? -
gertvdijk about 2 years@adosar Yes, the GNU/Linux program loader (libc) mandates that a file running code directly has the executable permissions set. The Wikipedia page on Shebang explains this well, imo. en.wikipedia.org/wiki/Shebang_(Unix)#Syntax