案例解析
这个问题描述起来有点违反直觉,要执行一个文件难道不应该需要可执行权限吗?让我们先来看一个例子:
# module1.py def test(): print ('hello world!') if __name__ == '__main__': test()
这是一个名为module1.py的文件,这个文件仅有可读权限:
[dechin@dechin-manjaro excute]$ ll
-r--r--r-- 1 dechin dechin 78 1月 15 17:06 module1.py
我们可以直接用python来运行一下这个文件:
[dechin@dechin-manjaro excute]$ python3 module1.py
hello world!
我们发现即使只有可读权限,这个文件也是可以运行的。为了严格验证,我们这里创建另外一种模式的测试,通过import来导入python文件,是否也不需要可执行权限呢?
# module2.py from module1 import test if __name__ == '__main__': test()
同样的,我们新建的文件也未赋予可执行权限:
[dechin@dechin-manjaro excute]$ ll
-r--r--r-- 1 dechin dechin 78 1月 15 17:06 module1.py
-r--r--r-- 1 dechin dechin 64 1月 15 17:44 module2.py
我们执行一下module2.py这个文件:
[dechin@dechin-manjaro excute]$ python3 module2.py
hello world!
那么我们的测试就完成了,经过验证,执行普通的py文件是不需要可执行权限的,这对我们的权限最小化约束就产生了一定的启发作用。
原理解释
在stackoverrun上面有一条回复,作者cedbeu是这样描述的:python本身承担了语言解析器的角色,py文件不过是一个文本文件,真正执行的二进制文件是python而不是用户所创建的py文件。因此,即使去掉py文件的可执行权限,该py文件也是可以通过python来执行的。但是,如果我们去掉了python的可执行权限,那就无法正常执行这一条任务了。
扩展测试
如果将py文件编译成pyc和pyo格式的文件,此时的任务执行是否需要可执行权限呢?首先测试pyc文件:
[dechin@dechin-manjaro excute]$ python3 -m py_compile module1.py
执行完编译,我们会在当前目录下发现一个__pycache__的文件夹,编译好的pyc文件就存储在这个目录下:
[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
总用量 4
-rw-r--r-- 1 dechin dechin 259 1月 15 18:01 module1.cpython-38.pyc
这里我们看到pyc文件的文件名会固定有个后缀,同样也没有可执行权限,这里我们用同样的命令来执行pyc文件:
[dechin@dechin-manjaro __pycache__]$ ll
-r--r--r-- 1 dechin dechin 259 1月 15 18:01 module1.cpython-38.pyc
-rw-r--r-- 1 dechin dechin 259 1月 15 18:13 module1.pyc
-r--r--r-- 1 dechin dechin 64 1月 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!
这里我们可以发现,不论是直接执行pyc文件,或者是改名为module1.pyc之后再通过module2.py导入的方式,都可以正常的被执行,而且都不具有可执行权限。接下来我们再尝试一下pyo文件:
[dechin@dechin-manjaro excute]$ python3 -O -m py_compile module1.py
执行带有opt的pyc文件:
[dechin@dechin-manjaro __pycache__]$ python3 module1.cpython-38.opt-1.pyc
hello world!
同样的,都可以正常的被执行,即使没有可执行权限。
技术彩蛋
即使我们把pyc文件强行改名为py文件,同样也是不影响任务执行的:
[dechin@dechin-manjaro __pycache__]$ cp module1.cpython-38.opt-1.pyc module1.py
[dechin@dechin-manjaro __pycache__]$ ll
总用量 20
-rw-r--r-- 1 dechin dechin 259 1月 15 18:17 module1.cpython-38.opt-1.pyc
-r--r--r-- 1 dechin dechin 259 1月 15 18:01 module1.cpython-38.pyc
-rw-r--r-- 1 dechin dechin 259 1月 15 18:20 module1.py
-rw-r--r-- 1 dechin dechin 259 1月 15 18:13 module1.pyc
-r--r--r-- 1 dechin dechin 64 1月 15 18:09 module2.py
[dechin@dechin-manjaro __pycache__]$ python3 module1.py
hello world!
以上是运行Python脚本时是否需要添加可执行权限?的详细内容。更多信息请关注PHP中文网其他相关文章!

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3汉化版
中文版,非常好用

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)