Windows Python Launcher Does Not Read `py.ini`

I currently have Python 3.4 as my default version of Python, but I want to temporarily install Python 2.7 as temporary.

I am on Windows 7 and my Python scripts are being run using the Windows Python launcher. The documentation says I can configure it by creating a py.ini file, but that will not work. I created a file with this content:

 [defaults] python=2.7 

I tried to put it in the same folder as the file that I am running, I tried to put it in C:\Users\Administrator\ , in C:\Users\Administrator\AppData\ and in C:\Users\Administrator\AppData\Local\ , but none of them worked. The launcher still uses Python 3.4. (Both when I double-click the file in the Windows user interface, and both when I launch the launcher directly, for example py my_file.py .)

Why py.ini Python Windows launcher ignore 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:\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 name to check their age: ") NameError: name 'raw_input' is not defined child process exit code: 1 
+5
source share
1 answer

The documentation for Python 3.5 describes this behavior:

The shebang /usr/bin/env line shape has another special property. Before looking for installed Python interpreters, this form will look for the PATH executable for the Python executable. This is consistent with the behavior of the Unix env program, which performs a PATH search.

Oddly enough, the same functionality applies to Python 3.4 (or at least version 3.4.3), despite the corresponding documentation page for Python 3.4 without mentioning it. I have included playback of this behavior 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 PATH system until C:\Python27 . You will say in the comment that

It is important that this particular script does not have a shebang

but the line

 parse_shebang: found command: python 

in your startup output, the fact that the script really needs to have a shebang string gives out.


I have Python 2.7.10 and Python 3.4.3 installed on my system, from 3.4 to 2.7 on PATH . I also have a py.ini file in C:\Users\Luke\AppData\Local , which contains the following:

 [defaults] python=2 

and a test.py script containing

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

I set the environment variable PYLAUNCH_DEBUG to 1 . By running this script with py test.py , I get the following output:

 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 

If I change my test.py script to

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

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

 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 
+1
source

All Articles