0

I am using windows 11 and have installed python 2.7 first, and python 3.10 right after. I have set the environment path for both.

I have also made a copy of the python exe and renamed them to "python2" and "python3" (see below)

https://i.imgur.com/oZlL2iS.jpeg

https://i.imgur.com/MBRe9LL.jpeg

In the command prompt when I type "python - - version" it displays the last version of python I installed which is python 3. And when I type "python - 2 - - version" it displays the python 2 version I installed. Everything is working as it's suppose. (see below)

https://i.stack.imgur.com/HB2sQ.png

Now at this point I created two different .py files (contents of files below)

https://i.stack.imgur.com/ABL80.jpg

https://i.stack.imgur.com/8O2op.jpg

The problem I am running into is that when I double click these python files, the command prompt opens and displays python 2.7 on both, even though I have the shebang line to associate with python 3 in one of the files.

In windows 10 which I set up many years ago, I have everything setup exactly the same, except for the python versions (I have python 2.7 and python 3.6). And when I double click the python 2 shebang file, the output is python 2.7, and when I double click the python 3 shebang file, it displays python 3.6.

How can I get this same outcome in windows 11? I'm not quite sure what the problem is.

I initially used the help from the answers given to the same question in windows 10 from many years ago, but these answers aren't working for windows 11. See below

How to run multiple Python versions on Windows

Richard
  • 1
  • 2

2 Answers2

0

As you are below python 3.7 you might have to use those shebang lines:

#! /usr/bin/python3.6
#! /usr/bin/python2.7

For this to work you have to install the python launcher

Beginngin with 3.7 you could loose the minor version and only use

#! /usr/bin/python3
Ovski
  • 575
  • 3
  • 14
0

Don't add either to the path and use the Python Launcher for Windows normally installed with Python:

    py -0             # List installed Pythons.
    py -2 script.py   # Run Python 2 (latest installed)
    py -3 script.py   # Run Python 3 (latest installed)

The environment variable PY_PYTHON=M.m, where M.m is the major/minor version, selects the default python to use for py script.py. You can also add .py;.pyw to the PATHEXT environment variable so you can just run script without py or an extension.

Use #!python2.7 at the top of scripts to override the default Python and use the specific version listed. #!\user\bin\python2.7 works too but doesn't need the full path on Windows. The full path is needed for the script work on *nix.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • This solution is not working on windows 11 – Richard Nov 12 '22 at 21:06
  • @Richard What does "is not working" mean? Are you using the standard installers from https://python.org? Just typing "py" at a command prompt should run Python with the default options. You *did* undo your mucking around with renaming and environment changes, right? – Mark Tolonen Nov 12 '22 at 21:13