简体   繁体   English

工具启动Python脚本中的shebang是从哪里来的?

[英]Where does the shebang in tool-launching Python scripts come from?

Several widely used Python tools — such as pip , yolk and ipython — are launched by scripts with a shebang specifying what Python instance should be used to run them. 脚本使用shebang启动了几种广泛使用的Python工具(例如pipyolkipython ,用于指定应使用哪个Python实例来运行它们。 How is the contents of this line determined? 如何确定此行的内容?


For example, on OS X machine, where I have been using Apple's Python, this line reads 例如,在我一直使用Apple Python的OS X机器上,此行显示为

#!/usr/bin/python

What would happen if I installed the python.org version, which is placed in 如果我安装了python.org版本,该怎么办?

/Library/Frameworks/Python.framework/Versions/2.7/python 

with a symlink at 在符号链接处

/usr/local/bin/python

and then updated or re-installed a tool? 然后更新或重新安装工具? If I updated a tool would the line change? 如果我更新了工具,线会改变吗? Would I have to fully reinstall (rather than simply updating) each tool to force a change? 我是否必须完全重新安装(而不是简单地更新)每个工具以进行更改? What would the line change to? 该行会变成什么? Would it be 可不可能是

#!/usr/local/bin

or 要么

#!/Library/Frameworks/Python.framework/Versions/2.7

Is there a way to make it 有没有办法做到这一点

#!/usr/bin/env python

The line comes from the sys.executable value . 该行来自sys.executable

If you symlink the python executable, and run Python from that symlink, sys.executable would use the new path. 如果您对python可执行文件进行符号链接,并从该符号链接运行Python,则sys.executable将使用新路径。 Updating or reinstalling the tool would reflect the changed executable path, yes. 是的,更新或重新安装该工具将反映更改后的可执行路径。

I am not aware of any method to make pip and friends use #!/usr/bin/env python instead. 我不知道有什么方法可以使pip和朋友使用#!/usr/bin/env python代替。 That'd be a bad idea, really, as that would make it harder to use the correct Python all the time, the one where the installed packages can be found needed to run the command-line script. 确实,这是一个坏主意,因为这将使一直使用正确的Python变得更加困难,可以在其中找到已安装的软件包来运行命令行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM