Installing and Using Python

To see if your computer has Python installed, try this:

  1. On a windows machine:

    In your My Documents folder (or the Desktop), use Notepad to make a file named Hello.py and place in the file the single line,

    raw_input("hello")
    
    Then, try double clicking on the file's icon to start the program. If it displays a new command window and prints hello, you have it.

    Or, from the Start menu, use Search to search all files and folders for the file, python.exe. If the search command finds it, it will show you where.

  2. On a Linux machine (or MacOS that lets you start a Unix command window): Open a command window and type, which python

    If Python exists, you will see the directory path to it. (Or, just type python, to see if the Python interpreter can be found and activated for you.)

Important: your computer must have Python version 2.4 or newer up to but not including any version of Python 3.X (which is incompatible with all previous versions, alas).

Installing Python if you do not have it

If you must install Python, you will find it easy: Go to http://www.activestate.com/activepython/downloads. Python is available for Windows, Mac OS/X, Linux, etc. Remember to download a 2.X version and not a 3.X version! (Note: Python is a standard utility for Linux, Ubuntu, and Cygwin, and you can also download Python from the same site where you downloaded any of these OS.)

Try your implementation by using Notepad to make a file named Hello.py (please, not Hello.py.txt!) and place within the file the single line,

raw_input("hello")
Then, try double clicking on the file's icon to start the program. If it starts and prints hello, you are good to go. (If you are a linux user, open a command window and type python Hello.py.) You should see hello print in the window.

How to develop and test Python programs

We get addicted to development environments, but they can be expensive and clunky. We don't need them for Python --- like Perl, Ruby, and Javascript, it is a scripting language, meant to be used with a text editor and a command window. (There do exist IDEs for Python, but there is nothing I can recommend.)

Also, we get addicted to double-clicking on icons to start applications. This doesn't work well when a Python program has hidden errors. This section explains how you can develop and test a Python program till it is ready to stand on its own and be double-clicked.

Linux users

Your life is an easy one: you will require an ordinary text editor and a command window. Your development cycle is this:
Repeat until happy:
    - use text editor to edit your program,  p.py
    - within the command window,  cd  to the folder where  p.py  lives
        and type   python p.py   and watch what happens
That's it. If you need to debug a program by inserting breakpoints, you do this manually by using the text editor to insert these commands at the breakpoint:
print myvariables    # where  myvariables  are the vars you want to see
raw_input("Press Enter to continue execution")   # pauses execution
Once you are happy with your apparently crash-free program, you can double click on its icon to start it. If you do this, add this last line to your program:
raw_input("Press Enter to finish")
Or, you can continue to run your programs from the command window.

Windows users

If you are a Windows user, you should use a simplistic text editor, like Notepad, to type your Python programs. Unfortunately, if your Python program contains an embedded error, then it is disaster to test it by double-clicking on its icon --- the program literally explodes its output window! There are two ways around this, one based on a test-harness program that you can double-click and the other is based on command windows.

Method 1: Python test-harness

Say you have a folder where your Python programs live. In that same folder, use Notepad to create this file, RunPython.py:
"""Test harness that traps all Python errors before quitting:"""

# to run an external program from within Python code do this:
# import subprocess
# subprocess.call(["path/program-name", "param1", "param2"])
# # e.g.,   subprocess.call(["C:/Python26/Python.exe", "p.py"])
 
import subprocess                               # imports Python utility module 
progname = raw_input("Type name of Python program: ")
subprocess.call(["python.exe", progname + ".py"])  # should work w.o. full path

raw_input("\nPress Enter key to terminate the application")

Now, say that you are developing a buggy program, p.py. Use the text editor to save p.py in the folder when RunPython.py lives. To test p.py, double-click on RunPython.py's icon. You will see an output window appear with this query:
Type name of Python program: 
Type p and hit Enter:
Type name of Python program: p
The test harness starts p.py as a subprocess, shows its output, and traps any exceptions (crashes) and displays them.

Your development cycle goes like this:

Repeat until happy:
    - use text editor to edit your program,  p.py
    - double-click on RunPython, type p, and watch what happens
That's it. If you need to debug a program by inserting breakpoints, you do this manually by using the text editor to insert these commands at the breakpoint:
print myvariables    # where  myvariables  are the vars you want to see
raw_input("Press Enter to continue execution")   # pauses execution

Once you are happy with your apparently crash-free program, you can double click on its icon to start it. If you do this, add this last line to your program:

raw_input("Press Enter to exit this application")
Or, you can continue to run your programs from RunPython.

Method 2: Windows-based command-window testing

You can test an insecure Python program by starting it within a Windows command window, like a Linux user would do. If the executing program generates an error, the error message is trapped by the command window and is displayed there.

First, try this experiement:

  1. open a command window
  2. type
    python
    

    If you see

    Python 2.6 (#1, May 18 2006, 07:40:45)
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    Then your computer is ready to use Python from the command window. But if you see the message,
    'python' is not recognized as an internal or external command,
    operable program or batch file.
    
    then you must tell Windows where to find the Python software that you installed. See the section below to fix things.

Finding Python

It is probably the case that Python is installed in a folder named C:\Python26, and within that folder, there is a program named python.exe. The path to Python is therefore C:\Python26. (Another possibility is that Python is installed at C:\Program Files\Python26.)

If you have no idea where Python was installed on your computer, you must search for it:

  1. Press Start in the lower left corner of your display; press Search; in the search window, press all files and folders; in the top textline that appears, type python.exe; press the Search button.
  2. After several minutes, the folder where Python is installed will be listed --- that folder name is the path to Python.

Now that you know the path to Python (e.g., C:\Python26), go look for it: Open a file window, find the folder, and open it --- is python.exe inside the folder? It should be! If not, double-check the path information --- you must find the location of the python.exe program to continue. Keep looking till you find it!

Now you are ready to use a command window to execute Python.

Setting a path in a command window

To use Python within a command window, do this: Open a new command window and type the set path command:

set path=C:\Python26
(Use the path you just discovered!) This tells the command window to find the Python interpreter, python.exe, in the folder, C:\Python26.

Now, type

python
You should see the start-up message for the Python interpreter. Success! Now, exit the interpreter. (Type exit) (If you do not see the start-up message, double-check the path to python.exe!)

Now you are ready. Your development cycle goes like this:

Repeat until happy:
    --- use the text editor to edit your program,  p.py
    --- within an open command window,  cd  to the folder where  p.py  lives,
        type   python p.py   and watch what happens
If you need to debug a program by inserting breakpoints, you do this manually by using the text editor to insert these commands at the breakpoint:
print myvariables    # where  myvariables  are the vars you want to see
raw_input("Press Enter to continue execution")   # pauses execution
Once you are happy with your apparently crash-free program, you can double click on its icon to start it. (If you do this, add this last line to your program:
raw_input("Press Enter to exit this application")
Or, you can continue to run your programs from the command window.

The permanent way to set the path

Each time you open a command window, you must type the set path command to set the path to Python. It is a bit annoying to do this every time you open a command window, and there is a clever repair --- you can reset permanently the value of Windows OS PATH variable so that Windows knows once and for all the path to Python. Here is what you must do on Windows XP to reset the PATH variable:

  1. press Start
  2. select Control Panel
  3. On the Control Panel window, in the left column, press Switch to classic view (If you don't see this text, go to the next step below.)
  4. Double click on the icon for System
  5. In the System window, click on Advanced
  6. Click on the button Environment Variables
  7. In the new window, scroll until you see the variable, Path; click on it
  8. Press Edit
  9. Carefully use the right arrow button to move to the end of the long sequence of folder names. At the very end, without including any extra blanks, type this:
    ;C:\Python26
    
Close all the windows and restart the computer. Now, when you open a command window, you can type python and the path is already known by WindowsXP.

If you use Windows Vista/Windows 7, you must still find your way to the Environment Variables table within your computer. Use your Help facility to search for information about Environment Variable.