Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between python shebangs with /usr/bin/env rather than hard-path?

I used to use the shebang

#!/usr/bin/env python

When is it better to use

#!/usr/bin/python

What is the exact difference between them?

like image 664
Ken Avatar asked Apr 18 '11 22:04

Ken


People also ask

What does usr bin env Python mean?

If you have installed many versions of Python, then #!/usr/bin/env ensures that the interpreter will use the first installed version on your environment's $PATH. If you are using Unix, an executable file that is meant to be interpreted can indicate what interpreter to use by having a #!

What is the difference between #!/ Usr bin bash and #!/ Bin bash?

If the shell scripts start with #!/bin/bash , they will always run with bash from /bin . If they however start with #!/usr/bin/env bash , they will search for bash in $PATH and then start with the first one they can find.


1 Answers

#!/usr/bin/python is hardcoded to always run /usr/bin/python, while #!/usr/bin/env python will run whichever python would be default in your current environment (it will take in account for example $PATH, you can check which python interpreter will be used with which python).

The second way ( #!/usr/bin/env python ) is preferred , as it's not dependent on particular installation. It will work for example with virtualenv setups or systems where there is no /usr/bin/python, but only e.g. /usr/local/bin/python.

like image 174
vartec Avatar answered Oct 09 '22 22:10

vartec