Why doesn't running a python file require execution permission?

Why run a python file does not require x permission when you run it as follows:

 python script.py 

But this happens when it starts as:

 ./script.py 
+5
source share
3 answers

Since you are working with python script.py , this is a python program; then it downloads and runs the script that you specified in the parameters, i.e. script.py (basically a text file). The script file does not have to be executable, because it runs the python interpreter (the binary python itself, which must have x resolution).

Using .\script.py you are trying to run your script directly (the same text file) as a program. When you do this, you want it to deal with the interpreter specified in the first line of your script code, "shebang", for example. #!/usr/bin/env python . If it is not installed with x resolution, the OS does not try to "execute" your file (although it may try to open it with the default program, where applicable), so this will not take care of shebang.

+7
source

The file itself is interpreted (read), and not executed in the first example. A python application is what requires execute rights.

In the second example, the file itself is executed, so these rights are required to execute it.

+4
source

When we run the script as python script.py , we actually python script.py interpreter, which is usually located in /usr/bin/python (the output of which python will tell you exactly).

The interpreter, in turn, reads the scripts and executes its code. This is an interpreter that has permission to execute.

When the script is executed as ./script.py , then the script is executed directly and therefore the script requires permission to execute. The interpreter used is set by the shebang line.

When the kernel detects that the first two bytes are #! then it uses the rest of the string as an interpreter and passes the file as an argument. Please note that for this the file must have permission to execute. In the first case, we indirectly do what the kernel will do if we execute the script as ./script.py

In short, to execute method 1, the interpreter only needs read permission, but to execute it later you need to execute it directly

+2
source

Source: https://habr.com/ru/post/1244653/


All Articles