Why is '#!/usr/bin/env python' supposedly more correct than just '#!/usr/bin/python'?

后端 未结 5 2087
悲哀的现实
悲哀的现实 2020-12-12 20:34

Anyone know this? I\'ve never been able to find an answer.

相关标签:
5条回答
  • 2020-12-12 20:50

    If you're prone to installing python in various and interesting places on your PATH (as in $PATH in typical Unix shells, %PATH on typical Windows ones), using /usr/bin/env will accomodate your whim (well, in Unix-like environments at least) while going directly to /usr/bin/python won't. But losing control of what version of Python your scripts run under is no unalloyed bargain... if you look at my code you're more likely to see it start with, e.g., #!/usr/local/bin/python2.5 rather than with an open and accepting #!/usr/bin/env python -- assuming the script is important I like to ensure it's run with the specific version I have tested and developed it with, NOT a semi-random one;-).

    0 讨论(0)
  • 2020-12-12 21:02

    it finds the python executable in your environment and uses that. it's more portable because python may not always be in /usr/bin/python. env is always located in /usr/bin.

    0 讨论(0)
  • 2020-12-12 21:08

    It finds 'python' also in /usr/local/bin, ~/bin, /opt/bin, ... or wherever it may hide.

    0 讨论(0)
  • 2020-12-12 21:09

    From wikipedia

    Shebangs specify absolute paths to system executables; this can cause problems on systems which have non-standard file system layouts

    Often, the program /usr/bin/env can be used to circumvent this limitation

    0 讨论(0)
  • 2020-12-12 21:11

    You may find this post to be of interest: http://mail.python.org/pipermail/python-list/2008-May/661514.html

    This may be a better explanation: http://mail.python.org/pipermail/tutor/2007-June/054816.html

    0 讨论(0)
提交回复
热议问题