IDLE is Python’s Integrated Development and Learning Environment.
24.6.2. Editing and navigation¶
In this section, ‘C’ refers to the key on Windows and Unix and the key on Mac OSX.
deletes to the left; deletes to the right
delete word left; delete word to the right
Arrow keys and / to move around
and moves by words
/ go to begin/end of line
/ go to begin/end of file
Some useful Emacs bindings are inherited from Tcl/Tk:
- beginning of line
- end of line
- kill line (but doesn’t put it in clipboard)
- center window around the insertion point
- go backward one character without deleting (usually you can also use the cursor key for this)
- go forward one character without deleting (usually you can also use the cursor key for this)
- go up one line (usually you can also use the cursor key for this)
- delete next character
Standard keybindings (like to copy and to paste) may work. Keybindings are selected in the Configure IDLE dialog.
18.104.22.168. 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, deletes up to 4 spaces if they are there. 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.
Completions are supplied for functions, classes, and attributes of classes, both built-in and user-defined. Completions are also provided for filenames.
The AutoCompleteWindow (ACW) will open after a predefined delay (default is two seconds) after a ‘.’ or (in a string) an os.sep is typed. If after one of those characters (plus zero or more other characters) a tab is typed the ACW will open immediately if a possible continuation is found.
If there is only one possible completion for the characters entered, a will supply that completion without opening the ACW.
‘Show Completions’ will force open a completions window, by default the will open a completions window. In an empty string, this will contain the files in the current directory. On a blank line, it will contain the built-in and user-defined functions and classes in the current namespaces, plus any modules imported. If some characters have been entered, the ACW will attempt to be more specific.
If a string of characters is typed, the ACW selection will jump to the entry most closely matching those characters. Entering a will cause the longest non-ambiguous match to be entered in the Editor window or Shell. Two in a row will supply the current ACW selection, as will return or a double click. Cursor keys, Page Up/Down, mouse selection, and the scroll wheel all operate on the ACW.
“Hidden” attributes can be accessed by typing the beginning of hidden name after a ‘.’, e.g. ‘_’. This allows access to modules with set, or to class-private attributes.
Completions and the ‘Expand Word’ facility can save a lot of typing!
Completions are currently limited to those in the namespaces. Names in an Editor window which are not via and will not be found. Run the module once with your imports to correct this situation. Note that IDLE itself places quite a few modules in sys.modules, so much can be found by default, e.g. the re module.
If you don’t like the ACW popping up unbidden, simply make the delay longer or disable the extension.
A calltip is shown when one types after the name of an acccessible function. A name expression may include dots and subscripts. A calltip remains until it is clicked, the cursor is moved out of the argument area, or is typed. When the cursor is in the argument part of a definition, the menu or shortcut display a calltip.
A calltip consists of the function signature and the first line of the docstring. For builtins without an accessible signature, the calltip consists of all lines up the fifth line or the first blank line. These details may change.
The set of accessible functions depends on what modules have been imported into the user process, including those imported by Idle itself, and what definitions have been run, all since the last restart.
For example, restart the Shell and enter . A calltip appears because Idle imports itertools into the user process for its own use. (This could change.) Enter and nothing appears. Idle does not import turtle. The menu or shortcut do nothing either. Enter and then will work.
In an editor, import statements have no effect until one runs the file. One might want to run a file after writing the import statements at the top, or immediately run an existing file before editing.
22.214.171.124. Python Shell window¶
interrupts executing command
sends end-of-file; closes window if typed at a prompt
(Expand word) is also useful to reduce typing
- retrieves previous command matching what you have typed. On OS X use .
- retrieves next. On OS X use .
- while on any previous command retrieves that command
126.96.36.199. 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, builtin class and function names, names following and , 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 popups and dialogs is not user-configurable.
24.6.3. Startup and code execution¶
Upon startup with the option, IDLE will execute the file referenced by the environment variables or . IDLE first checks for ; if is present the file referenced is run. If is not present, IDLE checks for . Files referenced by these environment variables are convenient places to store functions that are used frequently from the IDLE shell, or for executing import statements to import common modules.
In addition, also loads a startup file if it is present. Note that the Tk file is loaded unconditionally. This additional file is and is looked for in the user’s home directory. Statements in this file will be executed in the Tk namespace, so this file is not useful for importing functions to be used from IDLE’s Python shell.
188.8.131.52. Command line usage¶
If there are arguments:
- If , , or is used, all arguments are placed in and is set to , , or . No editor window is opened, even if that is the default set in the Options dialog.
- Otherwise, arguments are files opened for editing and reflects the arguments passed to IDLE itself.
184.108.40.206. IDLE-console differences¶
As much as possible, the result of executing Python code with IDLE is the same as executing the same code in a console window. However, the different interface and operation occasionally affect visible results. For instance, starts with more entries.
IDLE also replaces , , and with objects that get input from and send output to the Shell window. When this window has the focus, it controls the keyboard and screen. This is normally transparent, but functions that directly access the keyboard and screen will not work. If is reset with , IDLE’s changes are lost and things like , , and will not work correctly.
With IDLE’s Shell, one enters, edits, and recalls complete statements. Some consoles only work with a single physical line at a time. IDLE uses to run each statement. As a result, is always defined for each statement.
220.127.116.11. Running without a subprocess¶
By default, IDLE executes user code in a separate subprocess via a socket, which uses the internal loopback interface. This connection is not externally visible and no data is sent to or received from the Internet. If firewall software complains anyway, you can ignore it.
If the attempt to make the socket connection fails, Idle will notify you. Such failures are sometimes transient, but if persistent, the problem may be either a firewall blocking the connection or misconfiguration of a particular system. Until the problem is fixed, one can run Idle with the -n command line switch.
If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible.
Deprecated since version 3.4.
24.6.4. Help and preferences¶
18.104.22.168. Additional help sources¶
IDLE includes a help menu entry called “Python Docs” that will open the extensive sources of help, including tutorials, available at docs.python.org. Selected URLs can be added or removed from the help menu at any time using the Configure IDLE dialog. See the IDLE help option in the help menu of IDLE for more information.
22.214.171.124. Setting preferences¶
The font preferences, highlighting, keys, and general preferences can be changed via Configure IDLE on the Option menu. Keys can be user defined; IDLE ships with four built-in key sets. In addition, a user can create a custom key set in the Configure IDLE dialog under the keys tab.
IDLE contains an extension facility. Preferences for extensions can be changed with Configure Extensions. See the beginning of config-extensions.def in the idlelib directory for further information. The default extensions are currently:
Primaries represent the most tightly bound operations of the language. Their syntax is:
6.3.1. Attribute references¶
An attribute reference is a primary followed by a period and a name:attributeref ::= "."
The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the method. If this attribute is not available, the exception is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects.
A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object:subscription ::= "[" "]"
The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a method.
For built-in objects, there are two types of objects that support subscription:
If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.)
If the primary is a sequence, the expression (list) must evaluate to an integer or a slice (as discussed in the following section).
The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a method that interprets negative indices by adding the length of the sequence to the index (so that selects the last item of ). The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing occurs in the object’s method, subclasses overriding this method will need to explicitly add that support.
A string’s items are characters. A character is not a separate data type but a string of exactly one character.
A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or statements. The syntax for a slicing:slicing ::= "[" "]" slice_list ::= ("," )* [","] slice_item ::= | proper_slice ::=  ":"  [ ":"  ] lower_bound ::= upper_bound ::= stride ::=
There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice).
The semantics for a slicing are as follows. The primary is indexed (using the same method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose , and attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting for missing expressions.
A call calls a callable object (e.g., a function) with a possibly empty series of arguments:call ::= "(" [ [","] | ] ")" argument_list ::= ["," ] ["," ] | ["," ] | positional_arguments ::= ["*"] ("," ["*"] )* starred_and_keywords ::= ("*" | ) ("," "*" | "," )* keywords_arguments ::= ( | "**" ) ("," | "," "**" )* keyword_item ::= "="
An optional trailing comma may be present after the positional and keyword arguments but does not affect the semantics.
The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a method are callable). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal parameter lists.
If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is , it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don’t specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a exception is raised. Otherwise, the list of filled slots is used as the argument list for the call.
CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use to parse their arguments.
If there are more positional arguments than there are formal parameter slots, a exception is raised, unless a formal parameter using the syntax is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments).
If any keyword argument does not correspond to a formal parameter name, a exception is raised, unless a formal parameter using the syntax is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments.
If the syntax appears in the function call, must evaluate to an iterable. Elements from these iterables are treated as if they were additional positional arguments. For the call , if y evaluates to a sequence y1, …, yM, this is equivalent to a call with M+4 positional arguments x1, x2, y1, …, yM, x3, x4.
A consequence of this is that although the syntax may appear after explicit keyword arguments, it is processed before the keyword arguments (and any arguments – see below). So:
It is unusual for both keyword arguments and the syntax to be used in the same call, so in practice this confusion does not arise.
If the syntax appears in the function call, must evaluate to a mapping, the contents of which are treated as additional keyword arguments. If a keyword is already present (as an explicit keyword argument, or from another unpacking), a exception is raised.
Formal parameters using the syntax or cannot be used as positional argument slots or as keyword argument names.
Changed in version 3.5: Function calls accept any number of and unpackings, positional arguments may follow iterable unpackings (), and keyword arguments may follow dictionary unpackings (). Originally proposed by PEP 448.
A call always returns some value, possibly , unless it raises an exception. How this value is computed depends on the type of the callable object.
If it is—
- a user-defined function:
The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a statement, this specifies the return value of the function call.
- a built-in function or method:
The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods.
- a class object:
A new instance of that class is returned.
- a class instance method:
The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument.
- a class instance:
The class must define a method; the effect is then the same as if that method was called.