Setting up Python in Windows 7

An all-wise journalist once told me that “everything is easier in Linux,” and after working with it for a few years I’d have to agree — especially when it comes to software setup for data journalism. But …

Many newsroom types spend the day in Windows without the option of Ubuntu or another Linux OS. I’ve been planning some training around Python soon, so I compiled this quick setup guide as a reference. I hope you find it helpful.

Set up Python

Get started:

1. Visit the official Python download page and grab the Windows installer. Choose the 32-bit or 64-bit version, depending on your version of Windows 7 (right-click the Computer icon on your desktop and select Properties to find out which one you have). Note: Python currently exists in two versions, the older 2.x series and newer 3.x series (for a discussion of the differences, see this). This tutorial focuses on the 2.x series.

2. Run the installer and accept all the default settings, including the “C:\Python27″ directory it creates.


3. Next, set the system’s PATH variable to include directories that include Python components and packages we’ll add later. To do this:

  • Right-click Computer and select Properties.
  • In the dialog box, select Advanced  System Settings.
  • In the next dialog, select Environment Variables.
  • In the User Variables section, edit the PATH statement to include this:

 

C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;

4. Now, you can open a command prompt (Start Menu|Accessories or Start Menu|Run|cmd) and type:

 

C:\> python

That will load the Python interpreter:

 

Python 2.7.2  (default, Jun 12 2011, 14:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or license for more information.
>>>

Because of the settings you included in your PATH variable, you can now run this interpreter — and, more important, a script — from any directory on your system.

Press Control-Z to exit the interpreter and get back to a C: prompt.

Set up useful Python packages

1. setuptools offers the helpful easy_install utility for installing Python packages. Grab the appropriate version for your system and install.

2. pip is another package installer that improves on setuptools. Having pip and setuptools will cover most of your installation needs, so go ahead and add pip. Now that you’ve installed setuptools, you can add pip by typing this at any command prompt:

 

easy_install pip

Notice that easy_install executes without needing to be told where on the system it’s located. That’s the benefit of adjusting your PATH variable earlier.

3. Mechanize and BeautifulSoup are must-have utilities for web scraping, and we’ll add those next:

 

pip install mechanize
pip install BeautifulSoup==3.2

4. csvkit, which I recently covered here, is a great tool for dealing with comma-delimited text files. Add it:

 

pip install csvkit

You’re now set to get started using and learning Python under Windows 7. If you’re looking for a handy guide, start with the Official Python tutorial.

8 comments on “Setting up Python in Windows 7” »

  1. Any other useful utilities to add?

  2. You probably ought to tell ‘em specifically to download the older, 2.7 version, as 3.1 is listed as well.

    Looks awfully useful, though — thanks!

  3. Duly noted, Mike. Thanks!

  4. Thanks Anthony! As a long time Mac/Linux user, this tutorial was incredibly helpful in setting me up with a Python environment for my fresh Windows7 install.

  5. Glad it was helpful! A bunch of people are getting ready for some Django training at work this week, and someone passed this link around:

    http://docs.python-guide.org/en/latest/starting/install/win/

    Some good tips in there too.

  6. My python compiler is showing message:
    Traceback (most recent call last):
    File “F:\Python\latex2wp.py”, line 657, in
    s=extractbody(s)
    File “F:\Python\latex2wp.py”, line 140, in extractbody
    for i in range(1,(len(L)+1)/2) :
    TypeError: ‘float’ object cannot be interpreted as an integer
    >>>

    Please help

  7. Sumit,

    It seems that your code is attempting to load an object of type “float” into an integer. I’m guessing that’s due to the division operation occurring in the range method.

    You may want to explore StackOverflow or scan the Python docs for handing type casting.

Trackbacks

    Fresh from #nicar12, here are curated notes to set up a Windows 7 Python environment so I can practice at work | Madison.com Labs Blog

    [...] following are bullet points gathered from walkthroughs created by Anthony DeBarros and the Kenneth Reitz’s Python Guide to get a Python development environment up and running [...]




Comments? Questions?