[译]Why do people write #!/usr/bin/env python on the first line of a Python script?
2017-12-06 11:03
465 查看
If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH. The alternative would be to hardcode something like #!/usr/bin/python; that's ok, but less flexible. In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a #! at the start of the first line, followed by the interpreter (and any flags it may need). If you're talking about other platforms, of course, this rule does not apply (but that "shebang line" does no harm, and will help if you ever copy that script to a platform with a Unix base, such as Linux, Mac, etc).
如果你的电脑安装了多个版本的Python,/usr/bin/env将会确保解释器第一个是你环境的$PATH,替代的方法使用像#!/usr/bin/python硬编码的方式也可以,只是不太灵活。
在Unix系统中,在第一行用#!后面跟随解释器符号,来表明一个可是文件简要被什么解释器解释执行。
如果你的话说的是其他平台的话,当然这些规则并不适用,(但是"shebang行"并没有坏处,而且如果你将此脚本拷贝到类Unix的系统(Linux,Mac等)时会非常有帮助。)
相关文章推荐
- Why do you need to put #!/bin/bash at the beginning of a script file?
- Why do my text files suddenly have an additional at the end of each line?
- "#!/usr/bin/env python" vs "#!/usr/local/bin/python" -- 你造python文件第一行的三种写法吗?
- usr/bin/ld: cannot find -l<nameOfTheLibrary>
- #!/usr/bin/env python与#!/usr/bin/python的区别
- /usr/bin/env: ‘python’: No such file or directory
- #!/usr/bin/env python与#!/usr/bin/python的区别
- [置顶] python 中 #!/usr/bin/env python 与 #!/usr/bin/python的区别
- python升级带来的yum异常:File "/usr/bin/yum", line 30
- #!/usr/bin/python和#!/usr/bin/env的区别
- Python语言的首行注释 --- #!/usr/bin/env python与#!/usr/bin/python的区别
- If the root.sh script fails on a node during the install of the Grid Infrastructure with Oracle Data
- 阿里云 esc 云服务器无缘无故的一次/usr/bin 目录丢失导致整个服务无法启动 # ./shutdown.sh ./shutdown.sh: line 41: dirname:command not found cannot find /catalina.sh the find /catalina.sh The file is absent or does not have execute
- #!/usr/bin/env python与#!/usr/bin/python的区别
- #!/usr/bin/env python与#!/usr/bin/python的区别
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- OS X上修复The current contents of /usr/local are bin Cellar
- #!/usr/bin/env python与#!/usr/bin/python的区别
- Why do a lot of #defines in the kernel use do { ... } while(0)?
- #!/usr/bin/env python与#!/usr/bin/python