Bhishan Poudel Bhishan Poudel - 6 months ago 22
Python Question

Bash: How to give executable permission to all the python scripts in unix?

Suppose I have a python script called like this:

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

# Imports

# Script

I can run this scripts in two ways:

Using python interpreter:


Changing permission

chmod a+x; ./


How can I run any new or old python scripts without using
chmod a+x script_name
all the times.

I have root access and user access both to my computer.

Basically i want executable permission to all the .py files, how can we do so?

I tried different shebangs such as:

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

The python interpreter is also in the $PATH.
The output of echo $PATH is following:


Also, ls /usr/bin/py* has:

/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:

Permission Denied when executing python file in linux

bash permission denied for python

Permission denied when launch python script via bash


The hard way

Run below with root privilege:

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


chmod need not be run as root if you're the owner of .py file.

The smart way

Write a script to take care of this.

if [ -f "$1" ]
geany "$1" # You could also use xdg-open if you set geany to open .py files
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"

Save the script as, say, pycreator in say /usr/bin/ , then do

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

To create a new script using pycreator, do


Also [ this ] answer pointed to by @choroba in his comment provides valuable insight in this regard.