pythonpython-3.xcmdwindowlauncher

Multiple Python versions installed : how to set the default version for py.exe (Python Launcher for Windows) for CMD and for "Open with"


In brief:
I have two versions of Python installed: Python 3.6 and Python 3.9. I want to open all my .py and  .pyw (ex: when I double click on them) with Python Launcher for Windows. I want the default version of the Python launcher for Windows to be Python 3.6 so that the files without a shebang are open with Python 3.6. Python 3.9 should only be used for files with a shebang #! python3.9

When I right click on a file, choose open with and select C:\Windows\py.exe, then by default (without any shebang), it's Python 3.9 that is open. When I type py in a CMD I get Python 3.6.5.
But if I double click on C:\Windows\py.exe the console is Python 3.9.5.

How can I make sure that if I set open all .py' to C:\Windows\py.exe, then if they don't have a sheband they will be started with Python 3.6.5`?

In details :

I was running on Python 3.6 only until I installed Python 3.9 along side. I haven't yet updated all the package to Python 3.9 so I want the default Python to be 3.6. For my scripts that need to use Python 3.9, I use the shebang #! python3.9.

So I would like to set the Opens with... default to C:\Windows\py.exe but if I do that my scripts are open in Python 3.9.5 and so they failed.

What I have already done:

  1. I have created a C:\Windows\py.ini with the default set to python=3.6 (same for pyw).

  2. In path, I have move C:\Users\user\AppData\Local\Programs\Python\Python36\ above C:\Users\user\AppData\Local\Programs\Python\Python39.

  3. I have set another environment variable PY_PYTHON to PY_PYTHON=3.6.

  4. I have run assoc .py=Python

  5. The Windows documentation says that The py.exe launcher will automatically select the most recent version of Python you've installed, so I reinstalled Python 3.6 (after having already installed Python 3.9) but, still the default is set to Python 3.9 when I open a file with C:\WINDOWS\py.exe 

  6. ftype | find "Python" return this

    Python.ArchiveFile="C:\WINDOWS\py.exe" "%L" %*
    Python.CompiledFile="C:\WINDOWS\py.exe" "%L" %*
    Python.File="C:\WINDOWS\py.exe" "%L" %*
    Python.NoConArchiveFile="C:\WINDOWS\pyw.exe" "%L" %*
    Python.NoConFile="C:\WINDOWS\pyw.exe" "%L" %*

I am on Windows 10


Solution

  • 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:

    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:


    How to use it

    ● in a CMD:

    ● For python files:


    To verify that everything is working:


    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).