Question

What is the difference between these two lines?

#!/usr/bin/python

#!/usr/bin/env python

Was it helpful?

Solution

This has nothing to do per se with Python, more to do with the way shebang lines work.

In many UNIX-like systems, you are required to give absolute path names on the shebang line, such as /usr/bin/python.

But what happens when you move that script to a different machine that has Pyyhon in the /usr/local/bin directory? It won't work, that's what. Or, even if you just want to use a different Python interpreter in $HOME/python/bin for testing purposes, you need to change the shebang line.

env is a way to get around that. Since it's generally always in /usr/bin, you can safely include it in the shebang line as an absolute path.

The env command itself in this case searches the path for python and runs that executable file.

The env command actually can do more than that, it can print the environment (env) or it can modify it temporarily for a specific process (env xyzzy=plugh myprog) but, in this particular case, it's simply giving you the power to run Python out of your path rather than at a fixed location.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top