Multiple Python installations on one computer (Part 2)

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

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem

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

python

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:

C:\Python26\python.exe %*

(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:

python26.bat

C:\Python26\python.exe %*

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

apythonfile

instead of

apythonfile.py

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:

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW

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.

Advertisements

Setuptools and easy_install

If you’re like me, then you encountered modules asking you to use

easy_install

to install them. At first, it made no sense to me, but as I gained knowledge in Python, I finally understood it.

What you need to do is first install the setuptools.py scripts from PyPI. Go to http://pypi.python.org/pypi/setuptools and follow the simple installation instructions.

Once you have setuptools in your Python Scripts folder, you are meant to be able to simply call

easy_install <PACKAGE>

from the command line, but for some reason this didn’t work on my machine. Instead, I had to navigate to my Python installation directory’s scripts. for me it was

C:\Python26\Scripts\

Once in this directory, you can either type just

easy_install <PACKAGE>

or

easy_install.exe <PACKAGE>

depending on your preference.

Now let’s test our new installation of setuptools. Open up a command line and navigate to your Python Scripts directory, then type:

easy_install pygame

(No worries, this should not harm your pygame installation, in fact it may help you!)

You will get some output similar to:

C:\Python26\Scripts>easy_install pygame
Searching for pygame
Best match: pygame 1.9.1
Adding pygame 1.9.1 to easy-install.pth file

Using c:\python26\lib\site-packages
Processing dependencies for pygame

Note that if you do not already have the latest version of the chosen package, it will ouput a little more.

Basically what this program does is check the PyPI (Or cheeseshop) for your selected package. If it finds it, it scans the dependencies and files for it and installs them. If not, it looks harder and usually fails.

In case you see a module for Python that says it is on the PyPI, you can try using easy_install before manual installation, it really is easy.

Multiple Python installations on one computer (Part 1)

So, I now have four different Python installations running at the same time, they are:

Python 2.5

Python 2.6 (My preference)

Python 2.7a

Python 3.1

So far, I have not met any problems on this setup, except for a slight pain. .py files and friends are all associated with Python 2.6, which is what I want, but the “python” command line command is associated with Python 2.5. So, if I want to run a program in 2.5, I just type the python prefix, else, I just type the filename. There seems to be no real benefit nor drawback to having multiple installations as you will most likely end up only using one anyway. Even so, if you can properly manage your installations, I recommend having a setup similar to my own.

Keep in mind I’m still working this out, once I get it, I’ll be sure to post an update with the new info.
I also plan on making this into a series with useful info about this.

Introduction

Welcome to my new feed, Novice to Pro. Here I will post various findings I stumble upon in my journey from Novice to Pro.

I am an unproffesional programmer, I am self-taught and use Python primarily. Although I do know the basics of a handful of languages. Besides Python, I use C++, C#, Javascript, CSS, HTML, and a few others I can’t think of at the moment. When using Python, I normally write games using the excellent Pygame library. I’ve tried a few others like pyglet and rabbyt, but none of them seemto really compare to Pygame’s simplicity and power.
Anyway, enough rambling on about Python, let’s talk about this blog. Novice to Pro is aimed to be a place with all the knowledge I wish I had when I was learning. I’ve already found a few useful tips and tricks that I’ll be sharing shortly after this post, but keep in mind that NtP will be formuch more than just programming.

Now, if you’re intrigued, subscribe, if not, don’t. Just keep it in mind for a later date.