我目前使用Python 3.4作为我的默认Python版本,但我想暂时将Python 2.7设置为默认值。
我在Windows 7上,我的Python脚本使用Python Windows启动器运行。 The documentation says我可以通过创建py.ini
文件来自定义它,但这不起作用。我创建了一个包含以下内容的文件:
[defaults]
python=2.7
我已尝试将其放在与我正在运行的文件相同的文件夹中,我尝试将其放在C:\Users\Administrator\
,C:\Users\Administrator\AppData\
和C:\Users\Administrator\AppData\Local\
中,但这些都没有工作。启动程序仍然使用Python 3.4。 (当我双击Windows UI中的文件时,以及我直接启动启动器时都是这两种情况,例如py my_file.py
。)
为什么Python Windows启动程序会忽略我的py.ini
文件?
以下是运行py age.py
并设置了环境变量PYLAUNCH_DEBUG
的输出:
launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:\Users\Administrator\AppData\Local\py.ini'
File 'C:\Windows\py.ini' non-existent
Called with command line: age.py
maybe_handle_shebang: read 256 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 age.py'
Traceback (most recent call last):
File "age.py", line 17, in <module>
name = raw_input("Enter a person's name to check their age: ")
NameError: name 'raw_input' is not defined
child process exit code: 1
答案 0 :(得分:1)
documentation for Python 3.5描述了这种行为:
shebang line的
/usr/bin/env
形式还有一个特殊属性。在查找已安装的Python解释器之前,此表单将在可执行文件PATH
中搜索Python可执行文件。这与Unixenv
程序的行为相对应,后者执行PATH
搜索。
奇怪的是,尽管corresponding documentation page for Python 3.4没有提及,但同样的功能似乎也适用于Python 3.4(或至少版本3.4.3)。我已经在这个答案的底部包含了这种行为的复制品。
您的脚本似乎在顶部包含了shebang行#!/usr/bin/env python
,C:\Python34
在PATH
出现之前就在您的系统C:\Python27
上。你在评论中说
对于这个特定的剧本而言,没有一个shebang
非常重要
但行
parse_shebang: found command: python
在你的启动器输出中,它表明脚本必须确实有一个shebang行。
我的系统上安装了Python 2.7.10和Python 3.4.3,PATH
上的3.4之前有3.4。我在py.ini
中还有一个C:\Users\Luke\AppData\Local
文件,其中包含以下内容:
[defaults]
python=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