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
- Configuring Windows for Python
- Installing distribute, virtualenv and pip
Installing The Python Interpreter
This is likely to be the easiest part.
Go to the python.org download page and find an installer to download (at the time of this writing, the current version is 2.7.3)
- Choose the interpreter that matches your version of windows (X86-64 on 64 bit windows, the regular windows installer otherwise).
- If you don't know which windows you are running, choose the regular windows installer (not the X86-64 version). It will run on any recent version of windows
- Once you have downloaded an installer, open it and install like you would any normal Windows program
- It's probably easiest to choose the defaults unless you have a reason not to
- Assuming that you have chosen the defaults, you will likely have python installed at C:\Python27
- If it's not installed here, make note of this location as we'll need it later
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
- Click on the 'start' button
- right-click on "computer" and left-click on "properties"
- click on "Advanced System Settings"
- click on "Environment Variables"
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.
- Under "System Variables", click on "New ..."
- You could also use the "New ..." button under user variables if you aren't an administrator of your machine
- When the dialog for a new system variable comes up, type in the following:
- Variable Name: PYTHONPATH
- Variable Value: c:\Python27
- If you didn't install python to c:\Python27, use the path where you installed Python to
- Click "OK" to save the new environment variable
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)
- Find the 'Path' entry under 'System Variables' and click on it
- Click on the 'Edit ...' button
- Move the cursor to the end of the 'Variable Value:' and:
- If there is already text in the box, add a semicolon to the end
- Add 'C:\Python27;C:\Python27\Scripts' (without the quotes) to the end
- Again, if you installed Python elsewhere, use that location instead
- The C:\Python27\Scripts is needed for pip and virtualenv which we'll install in a minute
- Click 'OK'
Finishing Up System Variables
All the changes need to be saved before they will take effect.
- Click 'OK' on the 'Environment Variables' window
- Click 'OK' on the 'System Properties' window
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.
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.
Download distribute_setup.py somewhere that you have access to
- Open a command line (click on the start button and type 'cmd' (without quotes) into the search box)
- Navigate to the place you downloaded distribute_setup.py to
- For example, if your user name was Monty and you downloaded distribute_setup.py to your Desktop, type:
- cd \Users\Monty\Desktop\
- In the cmd window, type the following and hit 'enter'
- python distribute_setup.py
- You may see several "are you sure you trust this" warnings from windows. This is expected and you should be OK allowing the program to finish
Installing virtualenv and pip
Now that distribute is available, we can install virtualenv and pip using the easy_install tool which comes with distribute.
- In a cmd window (you can reuse the window from "installing distribute"), type:
- easy_install virtualenv
- Another cmd window and several windows warnings will show up, this is expected and you should be OK with allowing them
- After the first command has finished, type:
- easy_install pip
- As with virtualenv, there will be another window and some warnings. You should be OK with allowing them.
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.
For this part, I assume that you are:
Using a directory to hold development stuff. I'm going to use \Users\Monty\code but feel free to use whatever works for you - just remember to change the commands as needed
- Using cmd and have followed the steps listed above
Making a new virtualenv
- Open a cmd window
- Change to the directory you want to use
- cd \Users\Monty\code
- Create a new virtualenv named 'env_test'
- virtualenv --distribute env_test
- whether you use the '--distribute' argument is up to you. The author prefers distribute to setuptools due to better module dependency handling among other things but you are welcome to use what you wish
- virtualenv --distribute env_test
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
- From the directory containing 'env_test'
- Use the following command:
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
Installing something in the virtualenv
- From the window running the target virtualenv
- check for the '(env_test)' at the beginning of the line to make sure you won't install to the main system library
Use the following command to install pytest (a unit test framework)
- pip install pytest
- you can substitute any other package for pytest, this is just an example
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.