Reekdeb Mal
Reekdeb Mal

Reputation: 728

Using older Python 2.x while having both Python 2.x and 3.x installed on Windows

I'm new to python. I've installed both Python 3.2.2 for x64 and Python 2.7 for x86 on my 64-bit windows machine. I've got some python code that are coded for python 2.x versions. But every time I try to run them by double clicking it is interpreted by python 3.x.

How do I force them to use python version 2.7, may be using some directives or using a BATCH script?

Upvotes: 3

Views: 496

Answers (4)

Damian Yerrick
Damian Yerrick

Reputation: 4664

Going forward, the solution is to upgrade Python 3.2 to 3.3 or later and use the Python Launcher for Windows.

At the top of each Python 3 program, include the following line:

#!/usr/bin/env python3

At the top of each Python 2 program, include the following line:

#!/usr/bin/env python2

The #! part, called a shebang, indicates to Python Launcher which version of Python is desired. (It also indicates to UNIX that a program should be run with a particular interpreter instead of the shell.) The /usr/bin/env part helps locate the Python interpreter on your PATH when you run a program on UNIX. If you don't plan on using anything but Windows, you can leave it out:

#! python3

[or]

#! python2

Upvotes: 2

Adrien Plisson
Adrien Plisson

Reputation: 23303

open a command line window, and use "c:\python27\python.exe" yourscript.py (or whatever path your python 2.7 appears to be installed in).

of course, you can put that line into a batch file and execute the batch.

also, you can put a shortcut to c:\python27\python.exe on your desktop and drop your script onto that shortcut every time you want to run it.

Upvotes: 0

Abed Hawa
Abed Hawa

Reputation: 1362

try editing PATH environment variable, use python 2.7 path and delete python 3.2.2

Upvotes: -1

Fred Foo
Fred Foo

Reputation: 363607

You could, e.g., associate the file extension .py2 with Python 2.7 and rename the main file (assuming you regard Python 3.2 as your default version).

Upvotes: 1

Related Questions