9

I have python 2.6.6 and python 3.1.3 currently installed on my machine (Windows Vista 64 bit) My path variable includes the directory of both versions. How can I specify which python I want to run a program in. For instance, if I want to run a program in python 3, it works but if I want to run a different program in python2 I get a syntax error. So how can I run a python 2 program in the cmd?

Typing python in my command line, python 3.1.3 is the only one that shows up.

kachilous
  • 2,499
  • 11
  • 42
  • 56
  • possible duplicate of [How to run multiple python version on Windows](http://stackoverflow.com/questions/4583367/how-to-run-multiple-python-version-on-windows) – Piotr Dobrogost Dec 27 '13 at 21:40

6 Answers6

8

You can specify the version in the executable name python2.6 and python3.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • 6
    Except Windows doesn't do that. It is python.exe in both installations. You're thinking *nix. – Mark Tolonen Feb 09 '11 at 06:42
  • I also used the extension py3 for python3 and py2 for python 2, but it was too much to maintain. Pythin Launcher (see my answer below, is much better) – Ayman Nov 29 '11 at 05:24
6

Instead of just typing "python" on the command line, use the full path the python.exe that you want to run: FULL_PATH_TO_PYTHON_2.6.6\python.exe or FULL_PATH_TO_PYTHON_3.1.3\python.exe should distinguish between the two.

duffymo
  • 305,152
  • 44
  • 369
  • 561
4

You can also use: 'py -main_version script_name.py args'

Example:

py -2 script_name.py args for Python 2.X

py -3 script_name.py args for Python 3.X

To test both are working or not, you can try,

>> py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>> py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
imankalyan
  • 155
  • 1
  • 8
3

The Python Launcher is probably what you need. I used it with 2.7 and 3.2.

Ayman
  • 11,265
  • 16
  • 66
  • 92
0

The shell will read the PATH from left to right, so you most likely defined Python 3.1.3 before Python 2.6.6. Specify the full path for each to use both versions.

atx
  • 4,831
  • 3
  • 26
  • 40
0

If someone is using jupyter, and you have both python installed, you can also select which kernel to use

enter image description here

Alberto Perez
  • 1,019
  • 15
  • 17