954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

How to avoid python shebang line on linux.

0
By Gribouillis on Nov 28th, 2009 4:13 pm

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()" > mytest.py # create a python script in the current dir
chmod +x mytest.py # make it executable
./mytest.py # 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
fi

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

If you want your scripts to be portable you do.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

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

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: