You can teach your linux system that executable python files must be executed with /usr/bin/python. This allows you to write python scripts without the shebang line #!/usr/bin/env python . This snippet shows you how to do it !

# This is rather a shell code snippet than a python code snippet :)

# Your linux system can recognize that an executable file 
# with extension ".py" must be executed with the python interpreter.
# For this, issue the following command in a terminal (as root):

echo ':Python:E::py::/usr/bin/python:' > /proc/sys/fs/binfmt_misc/register

# Now test this:

echo "print dir()" > # create a python script in the current dir
chmod +x # make it executable
./ # run it

# You dont need the shebang line '#!/usr/bin/env python'
# in your scripts any more !!!!!

# Now we don't want to do this each time we log in. We can
# edit (as root) a file named rc.local. It can be  /etc/rc.local
# on your system. On mine this file is a symbolic
# link and the true file is /etc/rc.d/rc.local. Anyway,
# we add the following lines at the beginning of this file

if [ -e /proc/sys/fs/binfmt_misc ]; then
  echo ':Python:E::py::/usr/bin/python:' > /proc/sys/fs/binfmt_misc/register

# Now your system knows that ".py" files must be interpreted
# with /usr/bin/python.

Yes, but 99% of my scripts don't need to be portable. Also, if absolutely necessary, you can always call python .

Edited 7 Years Ago by Gribouillis: n/a

This is an addendum, because in my linux mint system, I could not write to /proc/sys/fs/binfmt_misc/register. The solution, which should also work in ubuntu, is to install the package binfmt-support with the software manager, then give the command

sudo update-binfmts --install Python /usr/bin/python --extension py

in a terminal. No rc.local or other configuration file manipulation is required.

Edited 3 Years Ago by Gribouillis

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.