To see if your computer has Python installed, try this:
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.
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.X, where X >= 4, but not Python 3.X,which is incompatible with all 2.X versions, alas.
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. Download a 2.7.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.
Also, we get addicted to double-clicking on icons to start applications. This doesn't always work 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.
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 happensThat'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 executionOnce 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.
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 several ways around this:
"""Test harness that traps all Python errors before quitting:""" import subprocess # imports Python utility module progname = raw_input("Type name of Python program: ") subprocess.call(["python.exe", progname + ".py"]) raw_input("\nPress Enter key to terminate the application")
Type name of Python program:Type p and hit Enter:
Type name of Python program: pThe 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 happensThat'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.
"""Test harness based on exception handling: """ try: import p ### patch the name of the program, p, HERE except: import traceback # traps and prints errors traceback.print_exc() print raw_input("press Enter key to finish")
First, try this experiement:
python
If you see
Python 2.7 (#1, May 18 2008, 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.
If you have no idea where Python was installed on your computer, you must search for it:
Now that you know the path to Python (e.g., C:\Python27), 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.
To use Python within a command window, do this: Open a new command window and type the set path command:
set path=C:\Python27(Use the path you just discovered!) This tells the command window to find the Python interpreter, python.exe, in the folder, C:\Python27.
Now, type
pythonYou 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 happensIf 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 executionOnce 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.
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 7 to reset the PATH variable:
;C:\Python27(or whatever path you have to the Python interpreter).