Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

python - 人们为什么在Python脚本的第一行上编写#!/ usr / bin / env python shebang?(Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?)

在我看来,如果没有该行,文件运行相同。

  ask by john garcias translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .

(如果安装了多个版本的Python,则/usr/bin/env将确保使用的解释器是环境的$PATH的第一个解释器。)

The alternative would be to hardcode something like #!/usr/bin/python ;

(另一种方法是对#!/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 #!

(在Unix中,要解释的可执行文件可以通过#!来指示要使用的解释器#!)

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).

(当然,如果您在谈论其他平台,则此规则不适用(但“ shebang行”没有害处,并且如果您将该脚本复制到具有 Unix基础的平台(例如Linux,Mac),将有帮助等)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...