Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper shebang for Python script

I'm usually using the following shebang declaration in my Python scripts:

#!/usr/bin/python 

Recently, I've came across this shebang declaration:

#!/usr/bin/env python 

In the script documentation, it was noted that using this form is "more portable".

What does this declaration mean? How come there's a space in the middle of the path? Does it actually contribute to protability?

like image 995
Adam Matan Avatar asked Jul 25 '13 00:07

Adam Matan


People also ask

Does Python need a shebang?

Whether using the shebang for running scripts in Python is necessary or not, greatly depends on your way of executing scripts. The shebang decides if you'll be able to run the script as a standalone executable without typing the python command. So, if you want to invoke the script directly – use the shebang.

What is correct for shebang statement?

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark ( #!) at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

Where do I put shebang Python?

shebang Definition The shebang character sequence is always used in the first line of any file. The statement that mentions the program's path is made by using the shebang character first and then the path of the interpreter program.

How do you use shebang in a script?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

Should Python2 only scripts be used in the shebang line?

python should be used in the shebang line only for scripts that are source compatible with both Python 2 and 3. in preparation for an eventual change in the default version of Python, Python 2 only scripts should either be updated to be source compatible with Python 3 or else to use python2 in the shebang line.

What is the purpose of Shebang in Python?

The purpose of shebang is for the script to recognize the interpreter type when you want to execute the script from the shell. Mostly, and not always, you execute scripts by supplying the interpreter externally. Example usage: python-x.x script.py This will work even if you don't have a shebang declarator.

What is a Shebang in C++?

The shebang is a unique character sequence found in script files, denoted by #!. By indicating the type of program that should be invoked, it helps to specify how the entire script should be run. Each file line begins with a shebang character sequence.

Should I use/usr/bin/env Python for writing a shebang?

If you write a shebang manually then always use #!/usr/bin/env python unless you have a specific reason not to use it. This form is understood even on Windows (Python launcher). Note: installed scripts should use a specific python executable e.g., /usr/bin/python or /home/me/.virtualenvs/project/bin/python.


1 Answers

#!/usr/bin/env python 

is more portable because in general the program /usr/bin/env can be used to "activate" the desired command without full path.

Otherwise, you would have to specify the full path of the Python interpreter, which can vary.

So no matter if the Python interpreter was in /usr/bin/python or in /usr/local/bin/python or in your home directory, using #!/usr/bin/env python will work.

like image 131
jh314 Avatar answered Sep 29 '22 10:09

jh314