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.
- A Python Windows Registry tutorial
- Some other article on MvP about Windows and Python
- Official Python documentation on Windows
- Another tutorial on Python on Windows
- PyWin32 documentation from ActiveState
22 thoughts on “Python 101: Setting up Python on Windows”
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.
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.
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 !
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!
Enthought has a free distribution that includes NumPy, SciPy, Matplotlib, and some other goodies. Â http://enthought.com/products/epd_free.php
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.
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.
Pingback: Frattanto nella blogosfera #48 « Ok, panico
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.
I also like to add “.PY” and “.PYW” to my PATHEXT environment variable, so that Windows will rocognize python files as “executable”.
Me too, but I forgot to mention that in my article.
That’s a great little utility–I use it all of the time.
I actually copy it to then end of the system variable string. Make sure you’re separating it from the previous one with a semi-colon
Pingback: Pythonç”Ÿæ€çŽ¯å¢ƒç®€ä»‹[è½¬] - Python - å¼€å‘è€…ç¬¬662332ä¸ªé—®ç”
Pingback: 38.Pythonå¼€å‘ç”Ÿæ€çŽ¯å¢ƒç®€ä»‹ - ç¼–ç¨‹è¯è¨€ç»¼åˆ - å¼€å‘è€…ç¬¬2233630ä¸ªé—®ç”
Pingback: Pythonå¼€å‘ç”Ÿæ€çŽ¯å¢ƒç®€ä»‹ – pythonåŸºç¡€æ•™ç¨‹
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.
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.
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.
Ugh. Compiling Python extensions on Windows is a total pain.
Pingback: ä¸€ç¯‡æ–‡ç« å…¥é—¨Pythonç”Ÿæ€ç³»ç»Ÿ - ç®—æ³•ç½‘
Comments are closed.