Case analysis
This question is a bit counterintuitive to describe. Shouldn't executable permissions be required to execute a file? Let's first look at an example:
# def test(): print ('hello world!') if __name__ == '__main__': test()
This is a file named, which has only readable permissions:
[dechin@dechin-manjaro excute]$ ll
-r--r--r-- 1 dechin dechin 78 January 15 17:06
We can run this file directly in python:
[dechin@dechin-manjaro excute]$ python3
hello world!
We found that this file can run even if it has readable permissions. In order to strictly verify, we create another test mode here to import python files through import. Does executable permissions do not require?
# from module1 import test if __name__ == '__main__': test()
Similarly, the new file we created does not give executable permissions:
[dechin@dechin-manjaro excute]$ ll
-r--r--r-- 1 dechin dechin 78 January 15 17:06
-r--r--r-- 1 dechin dechin 64 January 15 17:44
Let's execute this file:
[dechin@dechin-manjaro excute]$ python3
hello world!
Then our test is completed. After verification, executable permissions are not required to execute ordinary py files, which has a certain inspiration for our permission minimization constraints.
Explanation of principle
There is a reply on stackoverrun, the author cedbeu described it as follows: Python itself assumes the role of a language parser, the py file is just a text file, and the binary file that is actually executed is python rather than a py file created by the user. Therefore, even if the executable permissions of the py file are removed, the py file can be executed through python. However, if we remove the executable permissions of python, we will not be able to execute this task normally.
Extended testing
If you compile a py file into a pyc and pyo format file, does the task execution require executable permissions at this time? First test the pyc file:
[dechin@dechin-manjaro excute]$ python3 -m py_compile
After executing the compilation, we will find a __pycache__ folder in the current directory. The compiled pyc file is stored in this directory:
[dechin@dechin-manjaro excute]$ tree
.
├──
├──
└── __pycache__
└──
1 directory, 3 files
[dechin@dechin-manjaro excute]$ cd __pycache__/
[dechin@dechin-manjaro __pycache__]$ ll
Total dosage 4
-rw-r--r-- 1 dechin dechin 259 January 15 18:01
Here we see that the file name of the pyc file will be fixed with a suffix, and there is no executable permission. Here we use the same command to execute the pyc file:
[dechin@dechin-manjaro __pycache__]$ ll
-r--r--r-- 1 dechin dechin 259 January 15 18:01
-rw-r--r-- 1 dechin dechin 259 January 15 18:13
-r--r--r-- 1 dechin dechin 64 January 15 18:09
[dechin@dechin-manjaro __pycache__]$ python3
hello world!
[dechin@dechin-manjaro __pycache__]$ python3
hello world!
Here we can find that whether it is to directly execute the pyc file, or to rename it and then import it, it can be executed normally, and does not have executable permissions. Next, let's try the pyo file:
[dechin@dechin-manjaro excute]$ python3 -O -m py_compile
Execute a pyc file with opt:
[dechin@dechin-manjaro __pycache__]$ python3
hello world!
Similarly, they can be executed normally, even if there is no executable permission.
Technical Easter Egg
Even if we forcefully rename the pyc file to a py file, it will not affect the task execution:
[dechin@dechin-manjaro __pycache__]$ cp
[dechin@dechin-manjaro __pycache__]$ ll
Total dosage 20
-rw-r--r-- 1 dechin dechin 259 January 15 18:17
-r--r--r-- 1 dechin dechin 259 January 15 18:01
-rw-r--r-- 1 dechin dechin 259 January 15 18:20
-rw-r--r-- 1 dechin dechin 259 January 15 18:13
-r--r--r-- 1 dechin dechin 64 January 15 18:09
[dechin@dechin-manjaro __pycache__]$ python3
hello world!
This is the article about detailed explanation of whether Python execution py files requires executable permissions. For more related Python execution py files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!