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:\Users\Administrator\
, in C:\Users\Administrator\AppData\
and in C:\Users\Administrator\AppData\Local\
, 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:\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
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 executablePATH
for a Python executable. This corresponds to the behaviour of the Unixenv
program, which performs aPATH
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:\Users\Luke\AppData\Local
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:\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 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:\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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With