kachilous
kachilous

Reputation: 2529

How to run programs in python2 and python3

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.

Upvotes: 9

Views: 53427

Answers (6)

imankalyan
imankalyan

Reputation: 195

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.

Upvotes: 5

Alberto Perez
Alberto Perez

Reputation: 1077

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

enter image description here

Upvotes: 0

Ayman
Ayman

Reputation: 11585

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

Upvotes: 3

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95579

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

Upvotes: 8

atx
atx

Reputation: 5079

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.

Upvotes: 0

duffymo
duffymo

Reputation: 308938

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.

Upvotes: 6

Related Questions