Rushy Panchal
Rushy Panchal

Reputation: 17532

Dealing with Multiple Python Versions--- do not run independently

I have Python 2.7 (my preferred version) and 3.3 (version used in my Programming class) installed on my computer (OS: Windows 7). Until a certain point, they worked fine independently.

One day, one of my programs (in 3.3), got an error and crashed (it was in the command prompt, not in IDLE). After that, all of my 3.3 programs, when I attempt to edit them (edit with IDLE) or run them (in the command prompt) they open/run as 2.7 programs. This is an issue because of some of the syntax and other differences between 2.7 and 3.3.

How can I tell my 3.3 programs to explicitly use Python 3.3?

Right now, what I have to do is open IDLE (as a blank file, not a specific program), then use the Open option in IDLE. In addition, running the program with the command processor is out of the question.

I looked it up and saw something about using #!/usr/bin/env but I am unsure on how to use this. Python 3.3 is installed in the following folder: C:\Python3.3 (that's the file path).

Thanks!

Upvotes: 3

Views: 17759

Answers (5)

Soliloquy Design
Soliloquy Design

Reputation: 41

You can launch with a specific version of python from the command line by using 'py' followed by a version flag (i.e. -2 or -3)

Here is an example of what you could do:

Python 2

>>> py -2 your_script.py

OR

Python 3

>>> py -3 your_script.py

Upvotes: 4

pepr
pepr

Reputation: 20762

You should focus on the Windows specific features here. The #! way was usefull only for Unix based operating systems until recently.

Python 3.3 introduced Python Launcher for Windows for exactly that purpose. When Python 3.3 was installed on your machine, the installer copied py.exe and pyw.exe launchers into c:\Windows, and the .py and .pyw extensions were associated with the launchers.

This way, if the .py file is not launched explicitly via Python interpreter, the Python Launcher is used. You can also explicitly launch py script.py.

If there is no information about Python version, the highest Python 2.x is used. There are two ways how to tell the launcher that you want to launch Python 3: 1) explicitly py -3 script.py, 2) write #!python3 to the first line inside the script.py.

The wanted Python version can be more detailed (to execute the specific version of Python). See the mentioned doc. I have also summarized my surprise with that situation in the article Do you know the "Python Launcher for Windows"? at Experts Exchange.

Upvotes: 4

chidimo
chidimo

Reputation: 2948

The Python documentation for python 3.2 provides information which helped me to call different python 2.7 and 3.3 separately.

http://docs.python.org/3/using/windows.html#customizing-default-python-versions

Upvotes: 1

aw4lly
aw4lly

Reputation: 2155

You can invoke the python interpreter in Python3.3 by running py filename.py while if you run them in Python 2.7 you can just run python filename.py.

On my windows box I have small batch scripts (called python2 and python3) which call the relevant python source.

filename:    python3.bat
code:        C:\Python32\python.exe %*

Upvotes: 0

jfs
jfs

Reputation: 414199

You could use shebang lines:

#! /usr/bin/env python2

for Python 2.x scripts and:

#! /usr/bin/env python3

for Python 3.x scripts. You can use more specific versions e.g., python3.3

You can configure default Python version.

Upvotes: 7

Related Questions