Update: There is a possibly critical flaw with this technique. After setting this up, simply typing “apythonfile.py PARAMS” would not supply to params to sys.argv. Instead, I had to type “python apythonfile.py PARAMS”. I will update with a fix as soon as I find one, but it may involve registry editing.
Update2: Make sure not to REPLACE your PATH variable, if you do, just add
to it and it should fix it. (This is for Vista)
In part 1 of this series, I showed you the basics of having multiple Python installations on one computer without spending too much time.
In this part, I will show some useful techniques for enabling easier use of your installations.
So I’ll start off by showing you the basics of installing several versions at once. The first step is to decide which versions you want, a combination of 3.x and 2.x is a good idea. Once you’ve decided this, you should go to their various pages on the python website. After downloading the installers, be sure to run them in order of [b]least used to most used[/b]. Meaning that if you want Python26 to be your main (default) installation, run the 2.6 installer last. This will add certain file associations (Most of which will get overwritten in later parts of this series anyway, but whatever).
Ok, now that we have that out of the way, we need to set up our Python environment. Notice that at this point, with a fresh Python installation, using
will run a python interactive interpreter with your “default” version. We’re about to add to that. The first step is to create a directory with a meaningful name that you can’t forget. I chose C:/cmds. Once you have your directory picked out, you need to run any text editor, even notepad will do. Now, add to the file the line:
(If you are running on a system older than XP, you can use a neat little trick which is to add a @ before the “C”, this mutes the output of this file.) If you are running a different version as your default, then change the version numbers accordingly (Or if you have them in completely different directories for that matter, just make sure it points to a valid python interpreter.) Now save this file as “python.bat” in your chosen directory. Next, make a new .bat file called “pythonw.bat” and add a ‘w’ to the end of the “python” in the exe.
Now, you shouldn’t notice any real difference, that’s ok, we’ll fix that soon.
Next, you should go to your control panel and go to the edit environment variables section. To get there, go to System>Advanced System Settings (On the left)>The Environment Variables button (On the bottom right). Now you will see a sort of complicated window with useful variables that Windows uses. Let’s not hang around here too long. On the top, there should be a “PATH” variable (Under user variables). Select it and choose “Edit”. You may want to take note of the current path before changing it for later. Now change it to your chosen directory (Not that you could have also just put your commands in the old directory, but the new one is in a place you can easily edit). Now go ahead and click OK here to accept your changes. Now if you go back to the command line and type the command “python” you should see that it opens an interactive interpreter with your “default” python version. Now that’s not all we’re going to do here.
Now what we need to do is add commands like:
python26 apython26file.py python31 apython31file.py
To achieve this, we need to create some more .bat files in our directory. This part is pretty simple but can get repetitive. Start by opening up your original python.bat file and save it as a different version of Python. Example:
Now do the same for a “pythonw26.bat” file. (This time change “python.exe” to “pythonw.exe”)
If you have any more versions, do the same for them. You should end up with two .bat files for every Python version, which an extension to reflect the versions, plus two default ones with no version suffixes.
At this point, we’re done with the necessary stuff, but I’ve got a bonus for you! We’re going to be making it possible to use
It’s really unneeded, but can make typing in the command line easier later on.
So, to start, re-open that environment variables window you had open earlier. This time use the bottom panel and scroll down until you see the variable “PATHEXT”. This one we don’t want to completely override, instead, we need to append two things to it. To get an idea for just what you’re doing, look at the current contents. You will see that there are various file extensions in all caps separated by semicolons (“;”s). What we need to do is add the two extensions “.PY” and “.PYW”. When you’re done, you should have something like:
as the variable’s value. (Notice that your PATHEXT variable will likely be different from mine). Now click OK, open a new command line window (Did I mention that the changes won’t affect currently open windows?) and navigate to a python script. Try running it by just typing the filename minus the extension. It SHOULD work. If it didn’t work, you have a problem with file associations, which is a little more complicated than I think this already long post should go.
In Part 3, I will be addressing file extensions and setting their defaults, possibly with some more information.