Multiple versions of Python coexisting in the same OS

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Mon Jul 26 01:34:00 EDT 2010


On Mon, 26 Jul 2010 00:36:47 -0400, Edward Diener wrote:

> On 7/25/2010 10:42 PM, David Robinow wrote:
[...]
>> Edward, I'm having a really hard time understanding your problem. Could
>> you give an example of some real code that is causing you difficulty?
> 
> I start a Python script for version X by going to X's root directory and
> invoking 'python someScript.py' from the command line. Does that not
> sound reasonable ?

No it doesn't, it's a very unreasonable thing to do.

If you have multiple versions of Python, you should name them 
appropriately so you can launch the appropriate version from any 
directory:

python26 someScript.py  # calls python31 secondScript.py internally
python31 anotherScript.py  # calls python25 thirdScript.py internally

etc.

Or give the full path to the executable:

C:\Programs\python26\python.exe someScript.py  
# calls C:\Programs\python31\python.exe secondScript.py internally


If a script doesn't do this, then the script should be treated as buggy.



> In SomeScript.py there is an internal call to 'python someOtherScript.y
> someParameters'. 

That's a pretty dodgy thing to do in the first place, unless you can 
guarantee that there's only one executable called python. Otherwise, how 
do you know which one will be called? You can't easily predict which one 
will be called, so don't do it unless you want your scripts to call 
arbitrary executables.



> But the call invokes another version of Python because
> it is that version which is in the PATH. Or in SomeScript.py there is an
> internal call to 'someOtherScript.py someParameters'. But the call
> invokes another version of Python because the .py extension is
> associated with a different version.

Exactly. The root of your problem is that there are multiple executables 
called "python" and you don't know which one you will get. So don't do 
that.


> My solution is that I will write some code which sets a particular
> version of Python as the current version for a particular time, meaning
> that version will be in the PATH and associated with Python extensions.

/facepalm


> The way I do not have to worry when I externally invoke Python from the
> command line that internal calls are going to some other version.


Good luck with that.



-- 
Steven



More information about the Python-list mailing list