Setup Python on Windows

While there are certainly plenty of tutorials for installing Python on Windows, the idea here is to gather together the stuff that we generally use on top of just a regular Python install. For now, this will cover installation of python 2 instead of python3 but that may change in the future.

For our purposes, installing python consists of:

Installing The Python Interpreter

This is likely to be the easiest part.

At this point, you should be able to start Python via the "Python (command line)" entry in your start menu but in order to run python from the command line, a few system settings need to be changed.

Configuring Windows to Find Python

Instead of retyping an already good tutorial, follow the steps for listed in the configuring python section in the Using Python on Windows Tutorial on python.org.

The following instructions are meant to be followed straight through but have been broken up with small bits of explanation.

Configuring the PYTHONPATH

The PYTHONPATH variable is used to tell the Python interpreter where to look for libraries. By default, it should look where you've installed Python.

Adding Python and Python scripts to the PATH

On Windows machines, the PATH variable is used to tell the terminal where to look for executables. This will allow starting of the Python interpreter from the bare command line (which is useful, even if you don't use the command line much)

Finishing Up System Variables

All the changes need to be saved before they will take effect.

Remember that any open cmd windows will need to be closed in order to reload the new settings.

Installing Virtualenv and Pip

Virtualenv and pip are two utilities that make Python development much saner in general. They aren't strictly reqruied but any of our tutorials are generally going to involve virtualenv and we strongly recommend that you start off practicing good "Library Hygene" by keeping things separated when you can.

Virtualenv allows you to create pseudo system libraries so that you can try out any combination of python libraries and run code with them without worrying about messing up your base Python installation. Pip is a tool for easily installing and updating python packages listed on the Python Package Index (PyPI) which is just about anything you might be interested in.

Installing Distribute

Before we get to installing Virtualenv and Pip, we need to install distribute - a python library not in the base windows install but is required for both Virtualenv and Pip.

Installing virtualenv and pip

Now that distribute is available, we can install virtualenv and pip using the easy_install tool which comes with distribute.

Other Notes

If you're running powershell on Windows 7, the security policies need to be loosened to actually allow virtualenv to do the magic that it does so well. Rather than re-type what is already written in Virtualenv's installation docs, follow the instructions listed in the Virtualenv documentation for Windows.

Testing your new environment

You've been patient in following this so far, now let's test everything to make sure that it's working.

Assumptions

For this part, I assume that you are:

Making a new virtualenv

You should see something like the following while the virtualenv is created:

C:\Users\Monty\code>virtualenv --distribute env_test
New python executable in env_test\Scripts\python.exe
Installing distribute...........................................................
................................................................................
................................................................................
..done.
Installing pip..................done.

Activating the new Virtualenv

You are now using the 'env_test' virtualenv and should see a (env_test) before the directory you're currently in on the command line

(env_test) C:\Users\Monty\code>

Installing something in the virtualenv

You should see output similar to:

(env_test) C:\Users\Monty\code>pip install pytest
Downloading/unpacking pytest
  Downloading pytest-2.3.4.zip (527kB): 527kB downloaded
  Running setup.py egg_info for package pytest

Downloading/unpacking py>=1.4.12 (from pytest)
  Downloading py-1.4.12.zip (227kB): 227kB downloaded
  Running setup.py egg_info for package py

Installing collected packages: pytest, py
  Running setup.py install for pytest

    Installing py.test-script.py script to C:\Users\Monty\code\env_test\Scripts

    Installing py.test.exe script to C:\Users\Monty\code\env_test\Scripts
    Installing py.test-2.7-script.py script to C:\Users\Monty\code\env_test\Scr
ipts
    Installing py.test-2.7.exe script to C:\Users\Monty\code\env_test\Scripts
  Running setup.py install for py

Successfully installed pytest py
Cleaning up...

To test the pytest installation to figure out if it works, use:

And you should see output that starts with:

(env_test) C:\Users\Monty\code>py.test
============================= test session starts =============================
platform win32 -- Python 2.7.3 -- pytest-2.3.4
collected 68 items

... continues on ...

Unless you already have tests written for py.test, the command will likely end with some errors. Don't worry about this, it's supposed to do that.

SetupPythonOnWindows (last edited 2012-12-11 00:00:23 by tim)