Using Python from a command window: Setting the PATH variable

Say that you have installed Python on your computer. The standard way to use it to write and test programs is with a command window. (After you have the program operating correctly, then you can execute it by double-clicking on the program's icon.)

So, it is most important to learn how to start Python from a command window. First, try this experiement:

  1. open a command window
  2. type
    python
    

    If you see

    Python 2.4.3 (#1, May 18 2006, 07:40:45) 
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    Then your computer is ready to use Python and you need not read the rest of this note. 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 XP where to find the Python software that you installed.

Finding Python

When you installed Python, you told the installer program to place it somewhere on your disk. It is probably the case that there is a folder named C:\Python24, and within that folder, there is a program named python.exe. The path to Python is therefore C:\Python24. (Another possibility is that Python is installed at C:\Program Files\Python24.)

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:\Python24), 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.

The simple way

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

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

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 to use the same command window to cd to the folder where your Python programs reside, and you can use the Python interpreter to execute them.

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's PATH variable so that Windows XP 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:\Python24
    
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.