views:

331

answers:

5

What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?

A: 

Why do you want to do it? Everything that's in 3.0 is in 2.6, you can use the new features in 3.0 with from __future__ import. Run 2.6 until you're ready to upgrade to 3.0 for everything, then upgrade. You're not intended to have multiple versions of Python on the same machine. If you really want to, you can specify alternative install directories, but I really don't see how it's worth the hassle.

Dominic Rodger
Interesting, but I want to be able to learn the 3.0 syntax (print()) etc but still need to maintain some 2.5 and 2.6 code...
Christopher Mahan
-1. Try importing the _lack_ of a print statement. Or dictionary comprehensions.
Triptych
@Triptych - fair point.
Dominic Rodger
@Triptych Easy - `from __future__ import print_function`
dbr
@dbr i said lack of a print statement, not a print function.
Triptych
+8  A: 

No problem, each version is installed in its own directory. On my Windows box, I have C:\Python26\ and C:\Python31\. The Start Menu items are also distinct. Just use the standard installers from the Python Programming Language Official Website, or the ready-to-install distributions from ActiveState.

A direct way to select the wanted version is to name it explicitly on the command line.

C:\> C:\Python25\python ver.py
2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]

C:\> C:\Python31\python ver.py
3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]

Where ver.py is:

import sys
print (sys.version)
gimel
ok. so when I run the program I just prepend the path, right?Like c:/python26/python.exe myprog.pyWhat do you do with the .py extension?
Christopher Mahan
I hear you. That's the way I do it. The problem lies with the extension binding. I wish #! worked in windows.
Christopher Mahan
Look into using a modified %PATH% env variable for the different environments.Maybe just c:\python26\python myprog.py
gimel
> I wish #! worked in windows.Two options I can think of:Use a different extension for each version of python (E.g. foo.py3k or bar.py26)Create a simple 'python_selector.exe' that reads the source file for #! and runs the appropriate interpreter. Then associate all .py files with the selector.
brianpeiris
You could look into the Windows ASSOC and FTYPE commands to set a custom filetype (Python.File26, PYthon.File30) and then a bat file to change the association of .py between them. See http://bugs.python.org/issue4485
Zwergner
Ah, @brianpeiris and @Zwergner most excellent. I will dig further!
Christopher Mahan
One thing I do is use virtualenv (http://pypi.python.org/pypi/virtualenv). For each project I set up a virtualenv environment, based on the version of Python I need for that project. Then I just execute the activate.bat file and it sets the path approriately.
John Paulett
ok Johnp. I'll definitely take a look at virtualenv.
Christopher Mahan
All these answers involving batch files and ASSOC/FTYPE should be real answers to this question. That would make them easier to vote up and get you rep! :)
jmucchiello
+1  A: 

-You could use a batch file to launch with the appropriate version.

-Eclipse with pydev allows you to choose which version to launch with.

-Re-associate the .py/.pyw extension with your preferred version.

Will
I think I'm going to go with associating .py with version using ftype sort of thing mentioned by http://stackoverflow.com/users/124312/zwergner
Christopher Mahan
+1  A: 

Virtualenv is the solution of choice on Unix and Mac platforms.

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.4/site-packages (or whatever your platform's standard location is), it's easy to end up in a situation where you unintentionally upgrade an application that shouldn't be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.

I have not tried this, but the presence of documentation relating to use in Windows makes me think that it now works on Windows.

hughdbrown
Ok I think this is the solution I was waiting for,
Christopher Mahan
+1  A: 

Interesting, but I want to be able to learn the 3.0 syntax (print()) etc but still need to maintain some 2.5 and 2.6 code..

Python has __future__ "Future statement definitions", which make new features available in older versions of Python, the print function is one of them:

Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51)
>>> from __future__ import print_function
>>> print("Example", end=" Hurray!\n")
Example Hurray!

Another big Python 3.0 change is the default string becoming Unicode:

>>> from __future__ import unicode_literals
>>> type('abc')
<type 'unicode'>
>>> 'a'
u'a'

Most of the others are now part of Python 2.6, so aren't of interest (like the with_statement). The only one I didn't mention is from __future__ import braces to allow "C like" if(){} braces

dbr