Home > Backend Development > Python Tutorial > Do I need to add executable permissions when running a Python script?

Do I need to add executable permissions when running a Python script?

王林
Release: 2023-04-26 18:19:08
forward
1540 people have browsed it

Case Analysis

This problem is a bit counterintuitive to describe. Shouldn’t executable permissions be required to execute a file? Let us first look at an example:

# module1.py
def test():
    print ('hello world!')
 
if __name__ == '__main__':
    test()
Copy after login

This is a file named module1.py. This file only has readable permissions:

[dechin@dechin-manjaro excute ]$ ll
-r--r--r-- 1 dechin dechin 78 January 15 17:06 module1.py

We can directly use python to run this file:

[dechin@dechin-manjaro excute]$ python3 module1.py
hello world!

We found that this file can be run even if it only has read permissions . In order to strictly verify, we create another mode of test here to import the python file through import. Does it not require executable permission?

# module2.py
from module1 import test
 
if __name__ == '__main__':
    test()
Copy after login

Similarly, our newly created files are not given executable permissions:

[dechin@dechin-manjaro excute]$ ll
-r--r-- r-- 1 dechin dechin 78 January 15 17:06 module1.py
-r--r--r-- 1 dechin dechin 64 January 15 17:44 module2.py

Let’s execute the module2.py file:

[dechin@dechin-manjaro excute]$ python3 module2.py
hello world!

Then our The test is completed. It has been verified that executing ordinary py files does not require executable permissions, which has a certain inspiration for our permission minimization constraints.

Explanation of Principle

There is a reply on stackoverrun. The author cedbeu describes it like this: python itself assumes the role of language parser, and the py file is just a text file, the binary that is actually executed The file is a 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 still be executed through python. However, if we remove the executable permissions of python, we will not be able to perform this task normally.

Extended Test

If the py file is compiled into pyc and pyo format files, does the task execution at this time require executable permissions? First test the pyc file:

[dechin@dechin-manjaro excute]$ python3 -m py_compile module1.py

After executing the compilation, we will find it in the current directory A __pycache__ folder, the compiled pyc files are stored in this directory:

[dechin@dechin-manjaro excute]$ tree
.
├── module1 .py
├── module2.py
└── __pycache__
└── module1.cpython-38.pyc

1 directory, 3 files
[dechin@dechin- manjaro excute]$ cd __pycache__/
[dechin@dechin-manjaro __pycache__]$ ll
Total usage 4
-rw-r--r-- 1 dechin dechin 259 January 15 18:01 module1. cpython-38.pyc

Here we see that the file name of the pyc file will have a fixed suffix, and there is also 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 module1.cpython-38.pyc
- rw-r--r-- 1 dechin dechin 259 January 15 18:13 module1.pyc
-r--r--r-- 1 dechin dechin 64 January 15 18:09 module2.py
[dechin@dechin-manjaro __pycache__]$ python3 module1.cpython-38.pyc
hello world!
[dechin@dechin-manjaro __pycache__]$ python3 module2.py
hello world!

Here we can find that whether the pyc file is executed directly, or it is renamed module1.pyc and then imported through module2.py, it can be executed normally, and neither has executable permissions. Next, let’s try the pyo file again:

[dechin@dechin-manjaro excute]$ python3 -O -m py_compile module1.py

Execute with opt pyc file:

[dechin@dechin-manjaro __pycache__]$ python3 module1.cpython-38.opt-1.pyc
hello world!

Same , can be executed normally, even if there is no executable permission.

Technical Easter Egg

Even if we forcibly rename the pyc file to a py file, it will not affect the task execution:

[dechin@dechin-manjaro __pycache__] $ cp module1.cpython-38.opt-1.pyc module1.py
[dechin@dechin-manjaro __pycache__]$ ll
Total usage 20
-rw-r--r-- 1 dechin dechin 259 January 15 18:17 module1.cpython-38.opt-1.pyc
-r--r--r-- 1 dechin dechin 259 January 15 18:01 module1.cpython-38.pyc
-rw-r--r-- 1 dechin dechin 259 January 15 18:20 module1.py
-rw-r--r-- 1 dechin dechin 259 January 15 18:13 module1.pyc
-r--r--r-- 1 dechin dechin 64 January 15 18:09 module2.py
[dechin@dechin-manjaro __pycache__]$ python3 module1.py
hello world!

The above is the detailed content of Do I need to add executable permissions when running a Python script?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template