To set the default version of Python, you need to have installed Python Launcher (cf below for some info). If you didn't, you can probably install it by using the installation .exe of one of your Python installations, and selecting modifying/repair.
You also need to know where your Python Launcher is:
- if you installed it for all users, it is in
C:\WINDOWS\
,
- if not, it is in
%USERPROFILE%\AppData\Local\Programs\Python\Launcher\
.
In this example, the latest installed version of Python is Python 3.11 but the version that should be the default one is Python 3.6.
To set a default Python version:
go to your Python Launcher Folder and create 2 files: py.ini
and pyw.ini
make sure that both of these files are UFT-8 (they shouldn't be UFT-8 with BOM)
add to each of these files:
[defaults]
python=3.6
now, right click on any .py
, select open with
, select check another app
, check Always use this app...
, scroll all the way down and click on more app...
and navigate to your Python Launcher folder and select the py.exe
.
do the previous step again but this time, right-click on a .pyw
file and select the pyw.exe
.
How to use it
● in a CMD:
Do not use anymore python
in CMD
, always use py
. If you type just py
, then it will open a Python 3.6 console. If you want a Python 3.11 console, type py -3.11
.
As indicated in the comment by @karl-knechtel: If you have an active venv, using python instead of
py` will prioritize the venv's Python, which is generally what you want.
To use pip to install :
py -3.11 -m pip install # will install package for 3.11
py -m pip install # will install package for 3.6
● For python files:
- If the file doesn't contain a shebang, then it will be started with the default Python version which is
3.6
.
- To use
Python 3.11
for this file, add the shebang #!python3.11
at the very top of your script
To verify that everything is working:
open a CMD
, write py -0p
and look for the *
. It should be next to the default version you have set in the .ini
files.
Installed Pythons found by py Launcher for Windows
-3.11-64 C:\Users<user>\AppData\Local\Programs\Python\Python39\python.exe
-3.6-64 C:\Users<user>\AppData\Local\Programs\Python\Python36\python.exe *
With this setup, you can use the specific version of Python that you need in your scripts:
3.6 (as it will call the default version)
import sys
print(sys.version_info)
input("close")
3.6 (as it will call the default)
#!python
import sys
print(sys.version_info)
input("close")
3.11
#!python3.11
import sys
print(sys.version_info)
input("close")
3.11 (as it is the latest python 3 installed)
#!python3
import sys
print(sys.version_info)
input("close")
Some info about the Python launcher from docs.python.org
The Python launcher for Windows is a utility which aids in locating
and executing of different Python versions. It allows scripts (or the
command-line) to indicate a preference for a specific Python version,
and will locate and execute that version. Unlike the PATH variable,
the launcher will correctly select the most appropriate version of
Python. It will prefer per-user installations over system-wide ones,
and orders by language version rather than using the most recently
installed version.
Two .ini files will be searched by the launcher - py.ini in the
current user’s “application data” directory [...] and py.ini in the
same directory as the launcher. The same .ini files are used for both
the ‘console’ version of the launcher (i.e. py.exe) and for the
‘windows’ version (i.e. pyw.exe).
Customization specified in the “application directory” will have
precedence over the one next to the executable, so a user, who may not
have write access to the .ini file next to the launcher, can override
commands in that global .ini file.
⚠ Your python version can be installed either for all user (python.exe will be in program files) or for the profile (python.exe will be in appdata). If it doesn't work, try uninstalling all python version, and the launcher and reinstall everything as current user to start clean. To do that, download the .exe of your current Python installation, run it and click on uninstall (dont delete the remaining folder in app data if you want to keep your python packages), then reinstall it. If you install the launcher for all users but python for current user, your python.exe will be in appdata (a folder for each version), and your launcher will be C:\Windows\py.exe
If you want to install it for all "all users" and the check box is grayed out: you need first to uninstall the python launcher. If it's still grayed uninstall the python version(s) that remains (everything wil be in program files).
which py
result in? – Epochpy.exe
is supposed to be in the Windows folder. Please refer to the documentation. – Epochpy.exe
is supposed to be located in the Windows folder (when installed for all users). However the direct exe is located inside ofappdata\local\programs\Python\MyPythonVersion
. And the path variable should be linked here instead of to the Windows folder. – Charlatanismpy.exe
acts as a linker for multiple versions of python. You have to specify the version otherwise it chooses the first one found in the python path. I.E.py -3.9
– Charlatanismwhich py
returns/c/WINDOWS/py
– Odumpy.ini
in the folderC:\Users\<user>\AppData\Local\Programs\Python\Launcher
(with default set toPython 3.6
) and now it's working: I can setopen all .py with
toC:\WINDOWS\py.exe
and the default is python 3.6 and the shebang is taken into account. Please add this as an answer so I can select it! – Odum