This Appendix will cover how to set up Python 3 and introduce you to IDLE, the Python development environment we will be using throughout this book.[1]
Installing Python 3 and IDLE
Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. Even some Windows computers (notably those from HP) now come with Python already installed.
With that said, first of all check that you don’t already have Python installed by entering python in a command line window. If you see a response from a Python interpreter it will include a version number in its initial display. Generally any version of Python 3 will suffice.
If you need to install the Python 3 interpreter and IDLE software (IDE), you may as well download the most recent stable version. This is the one with the highest number that isn’t marked as an alpha or beta release. Please see the Python downloads page, https://www.python.org/downloads/ , for the most up to date versions of Python 3. They are available via the yellow download buttons on that page. For all downloads note the directory where the files will be saved to!
- If you’re running Windows: the most stable Windows downloads are available from the Python for Windows page, https://www.python.org/downloads/windows/ .
- If you’re running Windows XP: a complete guide to installing ActivePython is at Python on XP: 7 Minutes To “Hello World!”, http://dooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/ . ShowMeDo (https://wiki.python.org/moin/ShowMeDo ) has two videos for downloading (http://showmedo.com/videos/series?name=pythonOzsvaldPyNewbieSeries ), installing and getting started with Python on a Windows XP machine – this series talks you through the Python, ActivePython and SciPy distributions.
- If you are using a Mac, see the Python for Mac OS X page, https://www.python.org/downloads/mac-osx/ . MacOS 10.2 (Jaguar), 10.3 (Panther), 10.4 (Tiger) and 10.5 (Leopard) already include various versions of Python.
Once installed you should be all set to start using the Python development environment (IDLE) on your computer.
Using Python and its IDE
IDLE is Python’s Integrated Development and Learning Environment (IDE) and is included as part of the Python Standard Library which is distributed with Python 3. [2] IDLE is the standard Python development environment. Its name is an acronym of “Integrated DeveLopment Environment”. It works well on both Unix and Windows platforms.
It has a Python Shell window, which gives you access to the Python interactive mode. It also has a file editor that lets you create and edit existing Python source files.
IDLE’s menus dynamically change based on which window is currently selected. Each menu documented below indicates which window type it is associated with.
File menu (Shell and Editor)
New File
Create a new file editing window.
Open…
Open an existing file with an Open dialog.
Recent Files
Open a list of recent files. Click one to open it.
Open Module…
Open an existing module (searches sys.path).
Class Browser
Show functions, classes, and methods in the current Editor file in a tree structure. In the shell, open a module first.
Path Browser
Show sys.path directories, modules, functions, classes and methods in a tree structure.
Save
Save the current window to the associated file, if there is one. Windows that have been changed since being opened or last saved have a * before and after the window title. If there is no associated file, do Save As instead.
Save As…
Save the current window with a Save As dialog. The file saved becomes the new associated file for the window.
Save Copy As…
Save the current window to different file without changing the associated file.
Print Window
Print the current window to the default printer.
Close
Close the current window (ask to save if unsaved).
Exit
Close all windows and quit IDLE (ask to save unsaved windows).
Edit menu (Shell and Editor)
Undo
Undo the last change to the current window. A maximum of 1000 changes may be undone.
Redo
Redo the last undone change to the current window.
Cut
Copy selection into the system-wide clipboard; then delete the selection.
Copy
Copy selection into the system-wide clipboard.
Paste
Insert contents of the system-wide clipboard into the current window.
The clipboard functions are also available in context menus.
Select All
Select the entire contents of the current window.
Find…
Open a search dialog with many options
Find Again
Repeat the last search, if there is one.
Find Selection
Search for the currently selected string, if there is one.
Find in Files…
Open a file search dialog. Put results in a new output window.
Replace…
Open a search-and-replace dialog.
Go to Line
Move cursor to the line number requested and make that line visible.
Show Completions
Open a scrollable list allowing selection of keywords and attributes. See Completions in the Tips sections below.
Expand Word
Expand a prefix you have typed to match a full word in the same window; repeat to get a different expansion.
Show call tip
After an unclosed parenthesis for a function, open a small window with function parameter hints.
Show surrounding parens
Highlight the surrounding parenthesis.
Format menu (Editor window only)
Indent Region
Shift selected lines right by the indent width (default 4 spaces).
Dedent Region
Shift selected lines left by the indent width (default 4 spaces).
Comment Out Region
Insert ## in front of selected lines.
Uncomment Region
Remove leading # or ## from selected lines.
Tabify Region
Turn leading stretches of spaces into tabs. (Note: We recommend using 4 space blocks to indent Python code.)
Untabify Region
Turn all tabs into the correct number of spaces.
Toggle Tabs
Open a dialog to switch between indenting with spaces and tabs.
New Indent Width
Open a dialog to change indent width. The accepted default by the Python community is 4 spaces.
Format Paragraph
Reformat the current blank-line-delimited paragraph in comment block or multiline string or selected line in a string. All lines in the paragraph will be formatted to less than N columns, where N defaults to 72.
Strip trailing whitespace
Remove trailing space and other whitespace characters after the last non-whitespace character of a line by applying str.rstrip to each line, including lines within multiline strings.
Run menu (Editor window only)
Python Shell
Open or wake up the Python Shell window.
Check Module
Check the syntax of the module currently open in the Editor window. If the module has not been saved IDLE will either prompt the user to save or autosave, as selected in the General tab of the Idle Settings dialog. If there is a syntax error, the approximate location is indicated in the Editor window.
Run Module
Do Check Module (above). If no error, restart the shell to clean the environment, then execute the module. Output is displayed in the Shell window. Note that output requires use of print or write. When execution is complete, the Shell retains focus and displays a prompt. At this point, one may interactively explore the result of execution. This is similar to executing a file with python -i file at a command line.
Shell menu (Shell window only)
View Last Restart
Scroll the Shell window to the last shell restart.
Restart Shell
Restart the Shell to clean the environment.
Interrupt Execution
Stop a running program.
Debug menu (Shell window only)
Go to File/Line
Look on the current line. with the cursor, and the line above for a filename and line number. If found, open the file if not already open, and show the line. Use this to view source lines referenced in an exception traceback and lines found by Find in Files. Also available in the context menu of the Shell window and Output windows.
Debugger (toggle)
When activated, code entered in the Shell or run from an Editor will run under the debugger. In the Editor, breakpoints can be set with the context menu. This feature is still incomplete and somewhat experimental.
Stack Viewer
Show the stack traceback of the last exception in a tree widget, with access to locals and globals.
Auto-open Stack Viewer
Toggle automatically opening the stack viewer on an unhandled exception.
Options menu (Shell and Editor)
Configure IDLE
Open a configuration dialog and change preferences for the following: fonts, indentation, keybindings, text color themes, startup windows and size, additional help sources, and extensions (see below). On OS X, open the configuration dialog by selecting Preferences in the application menu. To use a new built-in color theme (IDLE Dark) with older IDLEs, save it as a new custom theme.
Non-default user settings are saved in a .idlerc directory in the user’s home directory. Problems caused by bad user configuration files are solved by editing or deleting one or more of the files in .idlerc.
Code Context (toggle)(Editor Window only)
Open a pane at the top of the edit window which shows the block context of the code which has scrolled above the top of the window.
Window menu (Shell and Editor)
Zoom Height
Toggles the window between normal size and maximum height. The initial size defaults to 40 lines by 80 chars unless changed on the General tab of the Configure IDLE dialog.
The rest of this menu lists the names of all open windows; select one to bring it to the foreground (deiconifying it if necessary).
Help menu (Shell and Editor)
About IDLE
Display version, copyright, license, credits, and more.
IDLE Help
Display a help file for IDLE detailing the menu options, basic editing and navigation, and other tips.
Python Docs
Access local Python documentation, if installed, or start a web browser and open docs.python.org showing the latest Python documentation.
Turtle Demo
Run the turtledemo module with example python code and turtle drawings.
Additional help sources may be added here with the Configure IDLE dialog under the General tab.
Context Menus
Open a context menu by right-clicking in a window (Control-click on OS X). Context menus have the standard clipboard functions also on the Edit menu.
Cut
Copy selection into the system-wide clipboard; then delete the selection.
Copy
Copy selection into the system-wide clipboard.
Paste
Insert contents of the system-wide clipboard into the current window.
Editor windows also have breakpoint functions. Lines with a breakpoint set are specially marked. Breakpoints only have an effect when running under the debugger. Breakpoints for a file are saved in the user’s .idlerc directory.
Set Breakpoint
Set a breakpoint on the current line.
Clear Breakpoint
Clear the breakpoint on that line.
Shell and Output windows have the following.
Go to file/line
Same as in Debug menu.
Automatic indentation
After a block-opening statement, the next line is indented by 4 spaces (in the Python Shell window by one tab). After certain keywords (break, return etc.) the next line is dedented. In leading indentation, Backspace deletes up to 4 spaces if they are there. Tab inserts spaces (in the Python Shell window one tab), number depends on Indent width. Currently, tabs are restricted to four spaces due to Tcl/Tk limitations.
See also the indent/dedent region commands in the edit menu.
Text colors
Idle defaults to black on white text, but colors text with special meanings. For the Shell, these are Shell output, Shell error, user output, and user error. For Python code, at the Shell prompt or in an editor, these are keywords, built-in class and function names, names following class and def, strings, and comments. For any text window, these are the cursor (when present), found text (when possible), and selected text.
Text coloring is done in the background, so uncolorized text is occasionally visible. To change the color scheme, use the Configure IDLE dialog Highlighting tab. The marking of debugger breakpoint lines in the editor and text in pop-ups and dialogs is not user-configurable.