Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - 我应该放#! (shebang)在Python脚本中,它应该采用什么形式?(Should I put #! (shebang) in Python scripts, and what form should it take?)

Should I put the shebang in my Python scripts?

(我应该把shebang放到我的Python脚本中吗?)

In what form?

(以什么形式?)

#!/usr/bin/env python 

or

(要么)

#!/usr/local/bin/python

Are these equally portable?

(这些同样便携吗?)

Which form is used most?

(最常用哪种形式?)

Note: the tornado project uses the shebang.

(注意: 龙卷风项目使用shebang。)

On the other hand the Django project doesn't.

(另一方面, Django项目没有。)

  ask by treecoder translate from so

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

1 Reply

0 votes
by (71.8m points)

The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python beforehand in the terminal or when double clicking it in a file manager (when configured properly).

(任何脚本中的shebang行都决定了该脚本具有像独立可执行文件一样执行的能力,而无需在终端中事先输入python或在文件管理器中双击它(如果配置正确)。)

It isn't necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they're looking at.

(不必要,但通常放在那里,因此当有人看到在编辑器中打开文件时,他们会立即知道他们在看什么。)

However, which shebang line you use IS important.

(但是,您使用的家当线非常重要的。)

Correct usage for Python 3 scripts is:

(Python 3脚本的正确用法是:)

#!/usr/bin/env python3

This defaults to version 3.latest.

(默认为版本3.latest。)

For Python 2.7.latest use python2 in place of python3 .

(对于Python 2.7.latest,请使用python2代替python3 。)

The following should NOT be used (except for the rare case that you are writing code which is compatible with both Python 2.x and 3.x):

(不应使用以下内容(除了极少数情况下,您正在编写与Python 2.x和3.x兼容的代码):)

#!/usr/bin/env python

The reason for these recommendations, given in PEP 394 , is that python can refer either to python2 or python3 on different systems.

(在PEP 394中给出这些建议的原因是python可以在不同系统上引用python2python3 。)

It currently refers to python2 on most distributions, but that is likely to change at some point.

(目前,在大多数发行版中都使用python2 ,但这在某些时候可能会改变。)

Also, DO NOT Use:

(另外,请勿使用:)

#!/usr/local/bin/python

"python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail."

(“在这种情况下,python可能安装在/ usr / bin / python或/ bin / python上,上面的#!将失败。”)

-- "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

(- “#!/ usr / bin / env python”与“#!/ usr / local / bin / python”)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...