The Python Windows launcher isn't reading `py.ini`

Question:

I currently have Python 3.4 as my default Python version, but I want to set Python 2.7 as the default one temporarily.

I’m on Windows 7 and my Python scripts are run using the Python Windows launcher. The documentation says I can customize it by creating a py.ini file, but that doesn’t work. I created a file with these contents:

[defaults]
python=2.7

I’ve tried placing it in the same folder as the file I’m running, I tried placing it in C:UsersAdministrator, in C:UsersAdministratorAppData and in C:UsersAdministratorAppDataLocal, but none of these worked. The launcher still uses Python 3.4. (Both when I double-click the file in the Windows UI and both when I launch the launcher directly, like py my_file.py.)

Why is the Python Windows launcher ignoring my py.ini file?

Here’s the output from running py age.py with the environment variable PYLAUNCH_DEBUG set:

launcher build: 32bit                                                                 
launcher executable: Console                                                          
Using local configuration file 'C:UsersAdministratorAppDataLocalpy.ini'          
File 'C:Windowspy.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:python34python.EXE                                                
located python on PATH: C:python34python.EXE                                        
run_child: about to run 'C:python34python.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  
Asked By: Ram Rachum

||

Answers:

The documentation for Python 3.5 describes this behaviour:

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.

Oddly enough this same functionality appears to apply to Python 3.4 as well (or at least version 3.4.3), despite the corresponding documentation page for Python 3.4 not mentioning it. I’ve included a reproduction of this behaviour at the bottom of this answer.

It seems your script contains the shebang line #!/usr/bin/env python at the top, and C:Python34 is on your system’s PATH before any appearance of C:Python27. You say in a comment that

It’s important for this specific script not to have a shebang

but the line

parse_shebang: found command: python  

in your launcher output gives away the fact that the script must indeed have a shebang line.


I have Python 2.7.10 and Python 3.4.3 installed on my system, with 3.4 before 2.7 on the PATH. I also have a py.ini file in C:UsersLukeAppDataLocal which contains the following:

[defaults]
python=2

and a test.py script that contains

#!/usr/bin/env python
import sys; print(sys.version_info)

I’ve set the value of the environment variable PYLAUNCH_DEBUG to 1. Running this script using py test.py, I get the following output:

launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:UsersLukeAppDataLocalpy.ini'
File 'C:WINDOWSpy.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:Python34python.EXE
located python on PATH: C:Python34python.EXE
run_child: about to run 'C:Python34python.EXE test.py'
sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
child process exit code: 0

If I change my test.py script to

#! python
import sys; print(sys.version_info)

(i.e. remove the /usr/bin/env from the shebang line) and re-run py test.py, I get the following:

launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:UsersLukeAppDataLocalpy.ini'
File 'C:WINDOWSpy.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:Python27python.exe is a 32bit executable
locate_pythons_for_key: C:Python27PCBuildpython.exe: The system cannot find the path specified.
locate_pythons_for_key: C:Python27PCBuildamd64python.exe: The system cannot find the path specified.
locate_pythons_for_key: C:Python34python.exe is a 32bit executable
locate_pythons_for_key: C:Python34PCBuildpython.exe: The system cannot find the path specified.
locate_pythons_for_key: C:Python34PCBuildamd64python.exe: The system cannot find the path specified.
found configured value 'python=2' in C:UsersLukeAppDataLocalpy.ini
search for default Python found version 2.7 at 'C:Python27python.exe'
run_child: about to run 'C:Python27python.exe test.py'
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
child process exit code: 0
Answered By: Luke Woodward
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.