78

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?

4

1 Answer 1

101

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

Sign up to request clarification or add additional context in comments.

Indirection via env also works when /usr/bin/python exists, but isn't an actual executable (e.g. it may be a launch script)
does that imply that it is better to use #!/usr/bin/env perl will be more versatile than the usual shebang #!/bin/perl? It seems to me that people usually use #!/bin/perl in perl, but #!/usr/bin/env python for python. Why?
@Ken, no principled reason, just accidents of history.
@Ken: AFAIK, there isn't popular equivalent of virtualenv for Perl.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.