简体   繁体   English

如何在 Linux 中为所有 Python 脚本授予可执行权限?

[英]How to give executable permission to all Python scripts in Linux?

Suppose I have a python script called a.py like this:假设我有一个名为a.py的 python 脚本,如下所示:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author    : Bhishan Poudel
# Date      : Jul 13, 2016


# Imports


# Script
print("hello")

I can run this scripts in two ways:我可以通过两种方式运行这个脚本:
Using python interpreter:使用python解释器:

python3 a.py

Changing permission更改权限

chmod a+x a.py; ./a.py

QUESTION问题
How can I run any new or old python scripts without using chmod a+x script_name all the times.如何在不始终使用chmod a+x script_name情况下运行任何新的或旧的 python 脚本。

I have root access and user access both to my computer.我对我的计算机具有 root 访问权限和用户访问权限。

Basically i want executable permission to all the .py files, how can we do so?基本上我想要所有 .py 文件的可执行权限,我们该怎么做?

I tried different shebangs such as:我尝试了不同的shebangs,例如:

#!/usr/bin/python3
#!/usr/bin/env python3
#!/usr/local/bin/python3
#!/usr/local/bin/env python3

The python interpreter is also in the $PATH. python 解释器也在 $PATH 中。 The output of echo $PATH is following: 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/

Also, ls /usr/bin/py* has:此外, 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@

Related links:相关链接:
http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm
Permission Denied when executing python file in linux 在linux中执行python文件时权限被拒绝
bash permission denied for python python的bash权限被拒绝
Permission denied when launch python script via bash 通过bash启动python脚本时权限被拒绝

The hard way艰难的道路

Run below with root privilege:使用 root 权限在下面运行:

find /your/path/ -type f -name "*.py" -exec chmod u+x {} \;

Note:注意:

chmod need not be run as root if you're the owner of .py file.如果您是.py文件的所有者,则无需以 root 身份运行chmod

The smart way聪明的方式

Write a script to take care of this.写一个脚本来解决这个问题。

#!/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

Save the script as, say, pycreator in say /usr/bin/ , then do将脚本保存为pycreator中的/usr/bin/ ,然后执行

chown root:root /usr/bin/pycreator
chmod +x-w /usr/bin/pycreator

To create a new script using pycreator , do要使用pycreator创建新脚本,请执行

pycreator calculator.py

Also [ this ] answer pointed to by @choroba in his comment provides valuable insight in this regard.此外, @choroba在他的评论中指出的[this]答案在这方面提供了宝贵的见解。

Using the idea of @sjsam, I did following:使用@sjsam的想法,我做了以下事情:

Suppose I have a file hello.py in any location.假设我在任何位置都有一个文件 hello.py。

cd to that location
find $PWD -type f -name "*.py" -exec chmod u+x {} \;
./hello.py

# Now, i can create any number of .py files in that folder and run ./filename

# Note: if we are running as user permission, and also have sudo access,
   we can also do:
   sudo -H find $PWD -type f -name "*.py" -exec chmod u+x {} \;

We should not use sudo unless absolutely necessary.

Thanks to sjsam.感谢 sjsam。

只需在终端中输入此命令即可。

sudo su

Try this chmod +x *.py it works on my PC(OS:Ubuntu 20.4), Also i am using #!试试这个chmod +x *.py它适用于我的 PC(操作系统:Ubuntu 20.4),我也在使用#! /usr/bin/env python3 shebang /usr/bin/env python3 shebang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM