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

3

1

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

Odum answered 24/6, 2021 at 19:48 Comment(11)
How are you currently launching python? And what is the reason for launching it from the Windows folder?Charlatanism
What does which py result in?Epoch
@Charlatanism py.exe is supposed to be in the Windows folder. Please refer to the documentation.Epoch
after you updated your path, did you open a brand new cmd window or are you using the one that you opened before making path changes? cmd windows do not dynamically update their env based on changes made is the system control.Coax
Yes py.exe is supposed to be located in the Windows folder (when installed for all users). However the direct exe is located inside of appdata\local\programs\Python\MyPythonVersion. And the path variable should be linked here instead of to the Windows folder.Charlatanism
So again how are you launching python, and why is it from the windows folder?Charlatanism
I realize now that py.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.9Charlatanism
@KarlKnechtel which py returns /c/WINDOWS/pyOdum
@JonSG, yes I did open a brand new cmd window (the problem is on the double click/open with not in cmd)Odum
@TheLazyScripter, thanks for all these info. You pointed me to the right direction with your comment about the second location. I simply added a copy of the py.ini in the folder C:\Users\<user>\AppData\Local\Programs\Python\Launcher (with default set to Python 3.6) and now it's working: I can set open all .py with to C:\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
@KJ, the ` "%L"` are not the cause of the problem but thanks for letting me know, this can indeed be a problem as per mkssoftware.com/docs/man1/ftype.1.aspOdum
O
6

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

Odum answered 26/6, 2021 at 6:0 Comment(4)
If you have an active venv, using python instead of py will prioritize the venv's Python, which is generally what you want.Epoch
@KarlKnechtel @MagTun. I have have set up py.ini and pyw.ini to default to 3.10 as described above. I also just installed 3.11b3 and now py.exe (in \windows) launches the beta. I can set PY_PYTHON=3.10, but that also changes python in a venv. I can use py -3.10, but I'd like to know why the .ini files aren't working. Any suggestions?Aardwolf
@Aardwolf please keep in mind that this is not a discussion forum. Please only use the comment section if there's something you don't understand about an answer. If you have your own particular situation that needs diagnosing, that is a new question. Any number of things could be wrong with the py.ini file, and the comment section is not designed to figure out that problem.Epoch
@KarlKnechtel Yes, I don't understand why the listed answer does not work for me. But I'll start a new top-level question.Aardwolf
C
4

If anyone is looking for this in 2023:

Goto your Environment Variables....

Under System variables, look for the variable PY_PYTHON. If it exists, you can change its value to the version of Python you want to use by default (for example, 3.10 or 3.11). If it doesn't exist, you can create it by clicking on New..., entering PY_PYTHON as the variable name, and the desired Python version as the variable value.

Click OK to apply the changes.

To verify your changes, open a new command prompt and type py. It should start the Python interpreter in the version you specified.

Convent answered 15/5, 2023 at 16:45 Comment(0)
S
1

After trying the above solutions, I had to make one more change as below;

  • OS: Windows 10
  • VSCode: 1.78.2
  • Installed Python Versions: 3.11 + 3.10 + 3.9

Somehow, VSCode was insisting to use 3.11 interpreter but I want to use 3.10. I found a config file in the virtual environment directory called "pyvenv.cfg", modified the file content like here

home = C:\Users\youruser\AppData\Local\Programs\Python\Python310
include-system-site-packages = false
version = 3.10.2
executable = C:\Users\youruser\AppData\Local\Programs\Python\Python310\python.exe
command = C:\Users\youruser\AppData\Local\Programs\Python\Python310\python.exe -m venv C:\path\to\venv

Then VSCode stick to python 3.10 interpreter in the virtual environment.

Scevour answered 30/5, 2023 at 16:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.