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.imgur.com/HPXqmIM.png
Now at this point I created two different .py files (contents of files below)
https://i.imgur.com/bl6THNk.jpeg
https://i.imgur.com/l3aTut6.jpeg
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
CodePudding user response:
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
CodePudding user response:
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.