Python 101: Setting up Python on Windows

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:

  1. 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:

  2. Go to the Advanced tab and press the Environmental Variable button to see something like the following:

  3. 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 file in it, all you need to do to install it is to type the following:

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

easy_install PackageName
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:

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.

Wrapping Up

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.

Further Reading

22 thoughts on “Python 101: Setting up Python on Windows”

  1. Yeah, those can be handy too. Good point. Of course, I don’t think any of those projects included all the handy tools I mentioned. It really depends on what you want, I guess.

  2. I don’t think I’ve ever heard of that tool. Thanks for mentioning that. I’ll put that on my list of things to check out.

  3. Mike, if case you need pywin32 and you are not installing Enthough or similar macropackages, then probably a good idea is to install the ActiveState Python distribution that already has pywin32 bundled, including its documentation.
    For win7, 64-bits, where not always 64-bit binary packages are available in the corresponding official repositories (or they are not free),  I use the ActiveState distribution, I install the 64-bit awesome Base package from Gohlke repository together with a few others such as Scipy, Numpy, matplotlib, pil… also in Gohlke’s, and that is !

  4. Yeah, I know people like the ActiveState one. I think I installed it at first too. I had forgotten it included the PyWin32 documentation. That is a good point. I’ll update the post with a link to those docs and some of these other packages. Thanks!

  5. Enthought has a free distribution that includes NumPy, SciPy, Matplotlib, and some other goodies.

  6. I thought they did at one point, but I just wasn’t finding it when I looked earlier. Thanks for the link. I updated my post to reflect that.

  7. That sounds like an interesting project, although I just use Wing’s IDE to switch between Python versions. This would probably be handy for people without a good IDE though.

  8. Pingback: Frattanto nella blogosfera #48 « Ok, panico

  9. default python (unfortunately) installation also makes FTYPE and ASSOC connection, so you can run python program by simply typing script’s filename into the shell.

    I say unfortunately, because, when you start using virtualenv (and you do), this is one thing you have to un-learn.

  10. I also like to add “.PY” and “.PYW” to my PATHEXT environment variable, so that Windows will rocognize python files as “executable”.

  11. Pingback: Python生态环境简介[转] - Python - 开发者第662332个问答

  12. Pingback: 38.Python开发生态环境简介 - 编程语言综合 - 开发者第2233630个问答

  13. Pingback: Python开发生态环境简介 – python基础教程

  14. This was a nice tutorial on setting up Python, but I don’t think it is necessary to mess with the system PATH environment variable. You could just have changed the user PATH variable and it would work as well since that just gets appended to the end of the system path. This way, you are not messing with the the sequence of the system’s path.

  15. Good point. This was written following some really old instructions I had found that may or may not have been the recommended method at the time.

  16. Right on. I had to explore this stuff over the weekend because I was trying to build a python module from source(what a pain:/), which required a C compiler. I ended up using pip to install the python module wheel file instead.

  17. Pingback: 一篇文章入门Python生态系统 - 算法网

Comments are closed.