2

I can change the order to python2,3 folders in the system PATH variable. But what are other ways to do this? rrr

There should be more elegant way to change versions of python i want to run.

e.g. in console:

python file.py #will run python2

and after i change python command to use python3, it should be the same:

python file.py #will use python3

ERJAN
  • 23,696
  • 23
  • 72
  • 146

2 Answers2

2

I suppose you are trying to run your script with the correct interpreter depending on which python version was used. On Unix/Linux this is done with a so called “shebang” which is written in the very first line of the file. E.g.:

#!/usr/bin/env python3.6

If your installation of Python3 is newer than Python 3.3 you can use the python launcher for windows, which should be able to launch the correct version of Python depending on the shebang, even on window.

Also see here for more informations on shebangs.

Matthias Brandt
  • 342
  • 3
  • 11
1

If your concern is what Python version is executed when calling python in a console, then an alias or a stub script are the two ways to go. This post will explain you how you can do this on Windows.

The alias way, just like it would be on a Unix system, is to create an alias, either temporary to the session or permanent, so that python now means C:\Python27\python, or whatever version you want.

The script approach consists in putting a script named python in a directory referred to in your PATH, and have that script run the right version of Python.

I highly doubt that this will affect all the batch scripts that call python, but it will definitely fire the right Python when you'll type python in a console.


Now, if you're concerned about what version a script is executed with, you can specify an explicit version with a shebang line, or manually select it by right-clicking the .py file and clicking open with.

Right leg
  • 16,080
  • 7
  • 48
  • 81
  • doskey.exe creates console aliases, which most people probably do not want, since they match in the console itself, not the shell, and only at the beginning of a line that's read from the console (e.g. you cannot pipe to an alias since `... | python` won't match the console alias). – Eryk Sun Oct 17 '18 at 17:51
  • Using Python 3's py.exe launcher is one common way to run different versions of Python, and associating it with .py files (as is configured by default) is the way for scripts to support shebangs. You can also create symlinks beside each executable (e.g. python3.6.exe -> python.exe) to allow running a particular version via `PATH`. – Eryk Sun Oct 17 '18 at 17:56