tags:

views:

177

answers:

6

I'm looking for a way to let multiple Python programs coexist on the same Windows machine.

Here's the problem: suppose program A needs Python 2.5, B needs 2.6, C needs 3, and each of them needs its own version of Qt, Wx or whatever other modules or whatever.

Trying to install all these dependencies on the same machine will break things, e.g. you can install different versions of Python side-by-side but only one of them can have the .py file association, so if you give that to Python 2.5 then B and C won't work, etc.

The ideal state of affairs would be if program A could live in C:\A along with its own Python interpreter, Qt/Wx/MySQL driver/whatever and never touch anything outside that directory, ditto for B and C.

Is there any way to accomplish this, other than going the full virtual box route?

edit: I tried the batch file solution, but it doesn't work. That is, it works on simple test scripts but e.g. OpenRPG fails at some point in its loading process if its required version of Python doesn't own the file association.

+1  A: 

One solution would be to craft a batch file that invokes the correct interpreter for a given application. THis way, you can install additional interpreters in separate folders.

Probably not perfect but it works.

jldupont
And the problem is that this is an incomplete solution to isolating installations and dependencies.
hughdbrown
Granted it doesn't beat VirtualEnv on Linux... or does VirtualEnv work on Windows too??
jldupont
Since the instructions give specific callouts for Windows usage, I would take it to mean that virtual env works on Windows. http://pypi.python.org/pypi/virtualenv Have I confirmed this? No. I'd even bet that it works somewhat differently under the Windows command interpreter than under Linux bash.
hughdbrown
Sorry @hughdbrown: I didn't see your post. My apologies. I use VirtualEnv on Linux myself and it is great... I didn't know it worked on Windows too. Again, sorry for any confusion/trouble etc.
jldupont
+2  A: 

Use batch files to run scripts, write in notepad for example:

c:\python26\python.exe C:\Script_B\B.py

and save it as runB.bat (or anything .bat). It will run with interpreter in c:\python26\python.exe file specified after a whitespace.

raceCh-
No, I haven't. Started writing it before you posted :)
raceCh-
A: 

write a python script that mimics the way unix shells handle scirpts -- look at the first line and see if it matches #!(name-of-shell). Then have your python script exec that interpreter and feed it the rest of its arguments.

Then, associate .py with your script.

Bryan Oakley
Unfortunately this fails in the general case where the Python source files don't begin with such a directive.
rwallace
+1  A: 

Have you considered compiling them to EXEs? Once you do that, all you have to do is call the EXE, for which the machine does not require python to be installed. All the required modules etc are packaged with the distribution when you compile.

inspectorG4dget
Yep, tried that, didn't work. Again using OpenRPG as a test case, I got through about half a dozen layers of problems with known solutions and another half dozen layers of problems with solutions I was able to guess by trial and error before giving up.
rwallace
+5  A: 

VirtualEnv.

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.

See previous answer here.

The other tool you should look at is pip which is great for installing particular versions of a library into a virtual environment. If you need to run v 1.0 of a library in python v 2.x for one application and 1.1 of the same library in python v 2.x, for example, you will need virtualenv plus a means of installing a particular version in that environment. Virtualenv + pip is your best choice.

hughdbrown
This looks like it could be a solution to the problem of different sets of modules within the same Python version. I don't think it tries to address the problem of different Python versions (and the resulting file association conflict), unless I'm misunderstanding it?
rwallace
As Ian Bicking says, "Whatever python interpreter you use to create the virtualenv, that's the interpreter that will be installed into the new environment." https://answers.launchpad.net/virtualenv/+question/21874 So you can get different python versions by creating the virtual env with a different python version.
hughdbrown
Have a +1 on me!
jldupont
Right, but it doesn't mention anything about the file association problem -- probably because it's talking about Mac OS, which for all I know uses a completely different file association technique. But looking at other replies, it looks like the ftype trick is a solution to that.
rwallace
So worst case, you don't run the script as `foo.py` but as `python foo.py` in your isolated environment and you pick up the right python version. The ftype association is the least of your problems if you are trying to get separate python versions running side-by-side.
hughdbrown
A: 

It looks like the best solution is a batch file that sets the file association before running the appropriate version of Python, as mentioned in the comments to one of the answers here: http://stackoverflow.com/questions/1515850/how-to-run-both-python-2-6-and-3-0-on-the-same-windows-xp-box

rwallace