When do you
#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python
Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.
Thus,
#/usr/bin/env python
Will figure out the correct location of python ( /usr/bin/python or /bin/python from $PATH) and make that as the interpreter for rest of the script.
- ( env is almost always located in /usr/bin/ so one need not worry what is env is not present at /usr/bin)
Hope this helps.
--
Senthil
from http://mail.python.org/pipermail/tutor/2007-June/054816.html
#/usr/bin/env python and #!/usr/local/bin/python
最新推荐文章于 2022-11-29 14:57:46 发布
本文解释了shebang行在Python脚本中的作用,特别是如何通过使用env命令来确保跨平台的兼容性,使脚本能够自动找到Python解释器的位置。
938

被折叠的 条评论
为什么被折叠?



