The /usr/bin/env form of shebang line has one further special property. Before looking for installed Python interpreters, this form will search the executable PATH for a Python executable. This corresponds to the behaviour of the Unix env program, which performs a PATH search.
奇怪的是,这个相同的功能似乎也适用于python3.4(或至少3.4.3版),尽管corresponding documentation page for Python 3.4没有提到它。我在这个答案的底部加入了这种行为的复制品。在
您的脚本似乎在顶部包含了shebang行#!/usr/bin/env python,而{}位于系统的PATH中的任何{}之前。你在评论中说It's important for this specific script not to have a shebang
但是那条线parse_shebang: found command: python
在启动程序的输出中,脚本必须有一个shebang行。在
我的系统上安装了Python2.7.10和Python3.4.3,在PATH上安装了2.7之前的3.4。我在C:\Users\Luke\AppData\Local中还有一个py.ini文件,其中包含以下内容:
^{2}$
以及一个test.py脚本,其中包含#!/usr/bin/env python
import sys; print(sys.version_info)
我已经将环境变量PYLAUNCH_DEBUG的值设置为1。使用py test.py运行此脚本,我得到以下输出:launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:\Users\Luke\AppData\Local\py.ini'
File 'C:\WINDOWS\py.ini' non-existent
Called with command line: test.py
maybe_handle_shebang: read 60 bytes
maybe_handle_shebang: BOM not found, using UTF-8
parse_shebang: found command: python
searching PATH for python executable
Python on path: C:\Python34\python.EXE
located python on PATH: C:\Python34\python.EXE
run_child: about to run 'C:\Python34\python.EXE test.py'
sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
child process exit code: 0
如果我将test.py脚本更改为#! python
import sys; print(sys.version_info)
(例如,从shebang行中删除/usr/bin/env)并重新运行py test.py,我得到以下结果:launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:\Users\Luke\AppData\Local\py.ini'
File 'C:\WINDOWS\py.ini' non-existent
Called with command line: test.py
maybe_handle_shebang: read 48 bytes
maybe_handle_shebang: BOM not found, using UTF-8
parse_shebang: found command: python
locating Pythons in 64bit registry
locate_pythons_for_key: unable to open PythonCore key in HKCU
locate_pythons_for_key: unable to open PythonCore key in HKLM
locating Pythons in native registry
locate_pythons_for_key: unable to open PythonCore key in HKCU
locate_pythons_for_key: C:\Python27\python.exe is a 32bit executable
locate_pythons_for_key: C:\Python27\PCBuild\python.exe: The system cannot find the path specified.
locate_pythons_for_key: C:\Python27\PCBuild\amd64\python.exe: The system cannot find the path specified.
locate_pythons_for_key: C:\Python34\python.exe is a 32bit executable
locate_pythons_for_key: C:\Python34\PCBuild\python.exe: The system cannot find the path specified.
locate_pythons_for_key: C:\Python34\PCBuild\amd64\python.exe: The system cannot find the path specified.
found configured value 'python=2' in C:\Users\Luke\AppData\Local\py.ini
search for default Python found version 2.7 at 'C:\Python27\python.exe'
run_child: about to run 'C:\Python27\python.exe test.py'
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
child process exit code: 0