Python Windows启动程序不读取`py.ini`

Ram*_*hum 5 python windows

我目前使用Python 3.4作为我的默认Python版本,但我想暂时将Python 2.7设置为默认值.

我在Windows 7上,我的Python脚本使用Python Windows启动器运行.文档说我可以通过创建py.ini文件来自定义它,但这不起作用.我创建了一个包含以下内容的文件:

[defaults]
python=2.7
Run Code Online (Sandbox Code Playgroud)

我已经尝试将它放在与我正在运行的文件相同的文件夹中,我尝试将其放入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  
Run Code Online (Sandbox Code Playgroud)

Luk*_*ard 4

Python 3.5 的文档描述了这种行为:

舍邦线的形式/usr/bin/env还有一个特殊的性质。在查找已安装的 Python 解释器之前,此表单将在可执行文件中搜索PATHPython 可执行文件。env这对应于执行搜索的Unix 程序的行为PATH

奇怪的是,同样的功能似乎也适用于 Python 3.4(或至少版本 3.4.3),尽管Python 3.4 的相应文档页面没有提及它。我在这个答案的底部包含了这种行为的再现。

看来您的脚本#!/usr/bin/env python在顶部包含 shebang 行,并且C:\Python34PATH出现任何C:\Python27. 你在评论中说

对于这个特定的脚本来说,重要的是不要有 shebang

但这条线

parse_shebang: found command: python  
Run Code Online (Sandbox Code Playgroud)

在你的启动器输出中揭示了这样一个事实:脚本确实必须有一个 shebang 行。


我的系统上安装了 Python 2.7.10 和 Python 3.4.3,其中 3.4 早于 2.7 PATH。我还有一个py.ini文件,其中C:\Users\Luke\AppData\Local包含以下内容:

[defaults]
python=2
Run Code Online (Sandbox Code Playgroud)

和一个test.py包含以下内容的脚本

#!/usr/bin/env python
import sys; print(sys.version_info)
Run Code Online (Sandbox Code Playgroud)

我已将环境变量的值设置PYLAUNCH_DEBUG1. 使用 运行此脚本py test.py,我得到以下输出:

#!/usr/bin/env python
import sys; print(sys.version_info)
Run Code Online (Sandbox Code Playgroud)

如果我将test.py脚本更改为

#! python
import sys; print(sys.version_info)
Run Code Online (Sandbox Code Playgroud)

(即从 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 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
Run Code Online (Sandbox Code Playgroud)