What is the line
#!/usr/bin/env python
in the first line of a python script used for?
In UNIX and Linux this tells which binary to use as an interpreter (see also Wiki page).
For example shell script is interpreted by /bin/sh
.
#!/bin/sh
Now with python it's a bit tricky, because you can't assume where the binary is installed, nor which you want to use. Thus the /usr/bin/env
trick. It's use whichever python binary is first in the $PATH
. You can check that executing which python
With the interpreter line you can run the script by chmoding it to executable. And just running it. Thus with script beginning with
#!/usr/bin/env python
these two methods are equivalent:
$ python script.py
and (assuming that earlier you've done chmod +x script.py
)
$ ./script.py
This is useful for creating system wide scripts.
cp yourCmd.py /usr/local/bin/yourCmd
chmod a+rx /usr/local/bin/yourCmd
And then you call it from anywhere just with
yourCmd