假设我有一个名为 a.py 的python脚本,如下所示:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author : Bhishan Poudel # Date : Jul 13, 2016 # Imports # Script print("hello")
我可以通过两种方式运行此脚本: 使用python解释器:
python3 a.py
变更权限
chmod a+x a.py; ./a.py
问题 如何在不使用chmod a+x script_name所有时间的情况下运行任何新的或旧的python脚本。
chmod a+x script_name
我对我的计算机具有root访问权限和用户访问权限。
基本上我想要所有.py文件的可执行权限,我们该怎么做?
我尝试了不同的shebang,例如:
#!/usr/bin/python3 #!/usr/bin/env python3 #!/usr/local/bin/python3 #!/usr/local/bin/env python3
python解释器也位于$ PATH中。echo $ PATH的输出如下:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Users/poudel/Applications/Geany.app/Contents/MacOS/:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Applications/Geany.app/Contents/MacOS/:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Applications/Geany.app/Contents/MacOS/
此外,ls / usr / bin / py *具有:
/usr/bin/pydoc* /usr/bin/python2.5@ /usr/bin/pythonw* /usr/bin/pydoc2.5@ /usr/bin/python2.5-config@ /usr/bin/pythonw2.5@ /usr/bin/pydoc2.6@ /usr/bin/python2.6@ /usr/bin/pythonw2.6@ /usr/bin/pydoc2.7@ /usr/bin/python2.6-config@ /usr/bin/pythonw2.7@ /usr/bin/python* /usr/bin/python2.7@ /usr/bin/python-config* /usr/bin/python2.7-config@
艰难的道路
以root权限运行以下命令:
find /your/path/ -type f -name "*.py" -exec chmod u+x {} \;
注意:
chmod如果您是.py文件的所有者,则无需以root用户身份运行。
chmod
.py
聪明的方法
编写脚本来解决这个问题。
#!/bin/bash if [ -f "$1" ] then geany "$1" # You could also use xdg-open if you set geany to open .py files else cp /path/to/python/startup/template "$1" # You may omit this if you don't have a default template chmod u+x "$1" geany "$1" fi
将脚本另存为,pycreator例如/usr/bin/,然后说
pycreator
/usr/bin/
chown root:root /usr/bin/pycreator chmod +x-w /usr/bin/pycreator
要使用创建新脚本pycreator,请执行
pycreator calculator.py