Proper shebang for Python script

PythonPortabilityShebang

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam MatanView Question on Stackoverflow
Solution 1 - Pythonjh314View Answer on Stackoverflow