Python 101: Setting Up on Windows
Join the DZone community and get the full member experience.Join For Free
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
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:
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:
- 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
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.