Thu 24 Nov 2011
Python is pretty easy to install on Windows, but sometimes you need to do a few extra tweaks to really get the most our your development environment. In this article, we will try to cover all the common things you might want to do or install to get an ideal Python Windows development workspace set up. Some of you might think that all you need to do is install Python and you’re done, but if you’re going to do Windows development, then you’ll need a few other packages to make it nicer.
Installing and Configuring Python
Download Python and run the installer. Make sure you got the version you want (i.e. Python 2.7 or 3.x). You’ll also want to make sure you get the bit type you want as there’s a 32-bit and a 64-bit version. Note: The 32-bit Python will run just fine on a 64-bit system, but not vice versa! Once you’ve got that installed, we’ll move on.
Now we’re going to make sure Python is set up right. Open a command window by going to Start –> Run and then typing “cmd” and pressing enter or return. Try typing “python” (without the quotes) in there and hitting your enter key. If you see the Python shell, then we’re halfway there. It should look something like this:
If you don’t, then we need to modify some settings.
Modifying Your Path
On Windows, it can help a LOT to modify your Path settings by adding your Python path and the path to the Scripts folder to your System Path. Here’s one way that you can do it:
- Right-Click “My Computer” and choose Properties (Windows XP) or you may have to go digging in Control Panel on Windows 7 and do a “Show All Control Panel Items” from the path bar and look for System. You should see something like this:
- Go to the Advanced tab and press the Environmental Variable button to see something like the following:
- You will see two types of variables here. In the bottom section are System Variables. Scroll down to the one labeled Path and at the end of that, add the following two lines: ;C:\Python26;C:\Python26\Scripts (adjust as necessary according to your version of Python and its location) Note that each entry is separated by a semi-colon, so make sure you do that too or this will not work!
Now you should be able to run Python from the command line. Not only that, you’ll also be able to run the special scripts that were in Python’s Scripts folder. Why would you care? Well, if you install easy_install / SetupTools or pip, they will install stuff in the Scripts folder that you can run. Speaking of which, since you now have a brand new installation of Python, let’s take a moment to show you how to install a 3rd party package.
How to Install a Package on Windows
We will use pip as our example. It is used for the easy installation of Python packages from the Python Packages Index (PyPI, not to be confused with PyPy). Anyway, you’ll need to go here to download it. The pip package is in a tarred and gzipped download at the bottom of the page. You’ll probably need something like IZArc or Filzip to unzip it since Windows doesn’t unzip tar files natively.
Open up a command line window as you did before. Now you will need to use the “cd” command to change directories to the location that you unzipped the files to. This can take some practice. On my machine, I usually unzip to my desktop and just cd there and then into the unzipped package. Once you’re in the folder that has the setup.py file in it, all you need to do to install it is to type the following:
python setup.py install
Most of the time, this works great and the package is installed correctly. Occasionally, you will run into certain packages that complain that they need a compiler. If you don’t have one installed (like Visual Studio or MingW), then you won’t be able to install the package this way. You will have to find a pre-packaged installer for it. On my machine, I usually have easy_install installed too. For some reason, the package is called SetupTools . Anyway, the main reason that you used to need easy_install was that it was the defacto standard for install Python Eggs, which you can read about here or here. Now you can use pip or distribute to do the same thing. All three can also install packages that are in compressed archives. Normally, all you have to do to install a package with one of these utilities is something like this:
pip install PackageName
Read their respective docs for more information though.
Other Handy Packages for Windows Developers
If you are a serious Windows developer who will need access to Windows APIs, then you will require the PyWin32 package. You’ll want to become familiar with ctypes too, but that’s been included with Python since version 2.5. PyWin32 is a lightweight wrapper around the Windows APIs. You can actually look on MSDN for the API documentation and almost directly translate it into Python. The ctypes package put on an even lower level and can be used to interact with DLLs directly, among many other things.
Another common need when programming on Windows is access to Window Management Instrumentation (WMI). Fortunately, Tim Golden has written a nice wmi module. You can usually get the same information using PyWin32, but it’s more convoluted than just using WMI. Tim Golden has also written several other utilities:
- WinSys – Python tools for the Windows Administrator
- winshell – a utility for getting Windows paths easily
- An Active Directory wrapper
You should also check out his awesome “How Do I” series of tutorials.
Finally, Python includes a module called _winreg that you can use to gain access to the Windows Registry. It’s a very powerful tool that’s very useful if you do a lot of administrative scripting on Windows.
Other Python Installations for Windows
The comments section started filling up with people mentioning the special Python installations you can get that actually include PyWin32 and other packages all wrapped up in one installer. That is also an option. Here are a few of those:
- PythonXY – This one includes a TON of other packages, including PyWin32, SciPy, wxPython, NumPy and dozens of others. It looks like a jack of all trades. I have not used this one, but it sounds interesting.
- The Enthought Python Distribution – That link is their paid version, but there’s also a lightweight free version available too.
- ActivePython from ActiveState – this one has been around for a long time and includes the PyWin32 documentation and various other tools.
You should now have all the tools you need to be an effective Windows developer with Python. If you need help, the PyWin32 and ctypes mailing lists are active and they have experts there that can answer just about anything you’d want to know about those packages.