<spanid="idle-python-editor-and-shell"></span><h1>IDLE — Python editor and shell<aclass="headerlink"href="#idle"title="Link to this heading">¶</a></h1>
<dt>Close Window</dt><dd><p>Close the current window (if an unsaved editor, ask to save; if an unsaved
Shell, ask to quit execution). Calling <codeclass="docutils literal notranslate"><spanclass="pre">exit()</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">close()</span></code> in the Shell
window also closes Shell. If this is the only window, also exit IDLE.</p>
<spanid="format-menu"></span><h3>Format menu (Editor window only)<aclass="headerlink"href="#format-menu-editor-window-only"title="Link to this heading">¶</a></h3>
<dt>Strip Trailing Whitespace</dt><dd><p>Remove trailing space and other whitespace characters after the last
non-whitespace character of a line by applying <aclass="reference internal"href="stdtypes.html#str.rstrip"title="str.rstrip"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">str.rstrip()</span></code></a> to each line,
<spanid="index-2"></span><h3>Run menu (Editor window only)<aclass="headerlink"href="#run-menu-editor-window-only"title="Link to this heading">¶</a></h3>
<dt>Run Module</dt><dd><p>Do <aclass="reference internal"href="#check-module"><spanclass="std std-ref">Check Module</span></a>. If no error, restart the shell to clean the
window. Note that output requires use of <codeclass="docutils literal notranslate"><spanclass="pre">print</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">write</span></code>.
This is similar to executing a file with <codeclass="docutils literal notranslate"><spanclass="pre">python</span><spanclass="pre">-i</span><spanclass="pre">file</span></code> at a command
<dt>Run… Customized</dt><dd><p>Same as <aclass="reference internal"href="#run-module"><spanclass="std std-ref">Run Module</span></a>, but run the module with customized
settings. <em>Command Line Arguments</em> extend <aclass="reference internal"href="sys.html#sys.argv"title="sys.argv"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">sys.argv</span></code></a> as if passed
<li><p><kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">LeftArrow</kbd> and <kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">RightArrow</kbd> moves left or right one word.</p></li>
<li><p><kbdclass="kbd docutils literal notranslate">Home</kbd> and <kbdclass="kbd docutils literal notranslate">End</kbd> go to the beginning or end of the line.</p></li>
<li><p><kbdclass="kbd docutils literal notranslate">Page Up</kbd> and <kbdclass="kbd docutils literal notranslate">Page Down</kbd> go up or down one screen.</p></li>
<li><p><kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">Home</kbd> and <kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">End</kbd> go to beginning or end of the file.</p></li>
<li><p><kbdclass="kbd docutils literal notranslate">Backspace</kbd> and <kbdclass="kbd docutils literal notranslate">Del</kbd> (or <kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">d</kbd>) delete the previous
<li><p><kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">Backspace</kbd> and <kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">Del</kbd> delete one word left or right.</p></li>
<li><p><kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">k</kbd> deletes (‘kills’) everything to the right.</p></li>
terminal newline removed. If <codeclass="docutils literal notranslate"><spanclass="pre">[x]</span><spanclass="pre">Regular</span><spanclass="pre">expression</span></code> is checked, the
and by a single click within the box. Close the box with <kbdclass="kbd docutils literal notranslate">Escape</kbd>,
<kbdclass="kbd docutils literal notranslate">Enter</kbd>, and double <kbdclass="kbd docutils literal notranslate">Tab</kbd> keys or clicks outside the box.
A double click within the box selects and closes.</p>
<p>One way to open a box is to type a key character and wait for a
predefined interval. This defaults to 2 seconds; customize it
in the settings dialog. (To prevent auto popups, set the delay to a
large number of milliseconds, such as 100000000.) For imported module
names or class or function attributes, type ‘.’.
For filenames in the root directory, type <aclass="reference internal"href="os.html#os.sep"title="os.sep"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">os.sep</span></code></a> or
<aclass="reference internal"href="os.html#os.altsep"title="os.altsep"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">os.altsep</span></code></a> immediately after an opening quote. (On Windows,
key is <kbdclass="kbd docutils literal notranslate">C</kbd>-<kbdclass="kbd docutils literal notranslate">space</kbd>. If one types a prefix for the desired name
<p>Within an editor window containing Python code, code context can be toggled
in order to show or hide a pane at the top of the window. When shown, this
pane freezes the opening lines for block code, such as those beginning with
<codeclass="docutils literal notranslate"><spanclass="pre">class</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">def</span></code>, or <codeclass="docutils literal notranslate"><spanclass="pre">if</span></code> keywords, that would have otherwise scrolled
out of view. The size of the pane will be expanded and contracted as needed
to show the all current levels of context, up to the maximum number of
lines defined in the Configure IDLE dialog (which defaults to 15). If there
are no current context lines and the feature is toggled on, a single blank
line will display. Clicking on a line in the context pane will move that
line to the top of the editor.</p>
<p>The text and background colors for the context pane can be configured under
the Highlights tab in the Configure IDLE dialog.</p>
<aclass="reference internal"href="exceptions.html#SyntaxError"title="SyntaxError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">SyntaxError</span></code></a> when multiple statements are compiled as if they were one.</p>
<p>IDLE also highlights the <aclass="reference internal"href="../reference/lexical_analysis.html#soft-keywords"><spanclass="std std-ref">soft keywords</span></a><aclass="reference internal"href="../reference/compound_stmts.html#match"><codeclass="xref std std-keyword docutils literal notranslate"><spanclass="pre">match</span></code></a>,
<aclass="reference internal"href="../reference/compound_stmts.html#match"><codeclass="xref std std-keyword docutils literal notranslate"><spanclass="pre">case</span></code></a>, and <aclass="reference internal"href="../reference/compound_stmts.html#wildcard-patterns"><codeclass="xref std std-keyword docutils literal notranslate"><spanclass="pre">_</span></code></a> in
pattern-matching statements. However, this highlighting is not perfect and
will be incorrect in some rare cases, including some <codeclass="docutils literal notranslate"><spanclass="pre">_</span></code>-s in <codeclass="docutils literal notranslate"><spanclass="pre">case</span></code>
<p>Upon startup with the <codeclass="docutils literal notranslate"><spanclass="pre">-s</span></code> option, IDLE will execute the file referenced by
the environment variables <spanclass="target"id="index-5"></span><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">IDLESTARTUP</span></code> or <spanclass="target"id="index-6"></span><aclass="reference internal"href="../using/cmdline.html#envvar-PYTHONSTARTUP"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">PYTHONSTARTUP</span></code></a>.
IDLE first checks for <codeclass="docutils literal notranslate"><spanclass="pre">IDLESTARTUP</span></code>; if <codeclass="docutils literal notranslate"><spanclass="pre">IDLESTARTUP</span></code> is present the file
referenced is run. If <codeclass="docutils literal notranslate"><spanclass="pre">IDLESTARTUP</span></code> is not present, IDLE checks for
<codeclass="docutils literal notranslate"><spanclass="pre">PYTHONSTARTUP</span></code>. Files referenced by these environment variables are
<spanclass="sig-name descname"><spanclass="pre">-c</span></span><spanclass="sig-prename descclassname"><spanclass="pre"><command></span></span><aclass="headerlink"href="#cmdoption-idle-c"title="Link to this definition">¶</a></dt>
<dd><p>Run the specified Python command in the shell window.
For example, pass <codeclass="docutils literal notranslate"><spanclass="pre">-c</span><spanclass="pre">"print('Hello,</span><spanclass="pre">World!')"</span></code>.
On Windows, the outer quotes must be double quotes as shown.</p>
<spanclass="sig-name descname"><spanclass="pre">-d</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-d"title="Link to this definition">¶</a></dt>
<dd><p>Enable the debugger and open the shell window.</p>
<spanclass="sig-name descname"><spanclass="pre">-e</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-e"title="Link to this definition">¶</a></dt>
<spanclass="sig-name descname"><spanclass="pre">-h</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-h"title="Link to this definition">¶</a></dt>
<dd><p>Print a help message with legal combinations of options and exit.</p>
<spanclass="sig-name descname"><spanclass="pre">-i</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-i"title="Link to this definition">¶</a></dt>
<spanclass="sig-name descname"><spanclass="pre">-r</span></span><spanclass="sig-prename descclassname"><spanclass="pre"><file></span></span><aclass="headerlink"href="#cmdoption-idle-r"title="Link to this definition">¶</a></dt>
<dd><p>Run the specified file in the shell window.</p>
<spanclass="sig-name descname"><spanclass="pre">-s</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-s"title="Link to this definition">¶</a></dt>
<dd><p>Run the startup file (as defined by the environment variables <spanclass="target"id="index-7"></span><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">IDLESTARTUP</span></code> or <spanclass="target"id="index-8"></span><aclass="reference internal"href="../using/cmdline.html#envvar-PYTHONSTARTUP"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">PYTHONSTARTUP</span></code></a>) before opening the shell window.</p>
<spanclass="sig-name descname"><spanclass="pre">-t</span></span><spanclass="sig-prename descclassname"><spanclass="pre"><title></span></span><aclass="headerlink"href="#cmdoption-idle-t"title="Link to this definition">¶</a></dt>
<spanclass="sig-name descname"><spanclass="pre">-</span></span><spanclass="sig-prename descclassname"></span><aclass="headerlink"href="#cmdoption-idle-0"title="Link to this definition">¶</a></dt>
<dd><p>Read and execute standard input in the shell window. This option must be the last one before any arguments.</p>
<li><p>If <codeclass="docutils literal notranslate"><spanclass="pre">-</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">-c</span></code>, or <codeclass="docutils literal notranslate"><spanclass="pre">-r</span></code> is used, all arguments are placed in <codeclass="docutils literal notranslate"><spanclass="pre">sys.argv[1:]</span></code>,
and <codeclass="docutils literal notranslate"><spanclass="pre">sys.argv[0]</span></code> is set to <codeclass="docutils literal notranslate"><spanclass="pre">''</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">'-c'</span></code>, or <codeclass="docutils literal notranslate"><spanclass="pre">'-r'</span></code> respectively.
No editor window is opened, even if that is the default set in the <em>Options</em> dialog.</p></li>
<li><p>Otherwise, arguments are treated as files to be opened for editing, and <codeclass="docutils literal notranslate"><spanclass="pre">sys.argv</span></code> reflects the arguments passed to IDLE itself.</p></li>
usually displays a <codeclass="docutils literal notranslate"><spanclass="pre">Tk</span></code> error box with a ‘cannot connect’ message
that directs the user here. It then exits.</p>
<p>One specific connection failure on Unix systems results from
misconfigured masquerading rules somewhere in a system’s network setup.
When IDLE is started from a terminal, one will see a message starting
with <codeclass="docutils literal notranslate"><spanclass="pre">**</span><spanclass="pre">Invalid</span><spanclass="pre">host:</span></code>.
The valid value is <codeclass="docutils literal notranslate"><spanclass="pre">127.0.0.1</span><spanclass="pre">(idlelib.rpc.LOCALHOST)</span></code>.
One can diagnose with <codeclass="docutils literal notranslate"><spanclass="pre">tcpconnect</span><spanclass="pre">-irv</span><spanclass="pre">127.0.0.1</span><spanclass="pre">6543</span></code> in one
terminal window and <codeclass="docutils literal notranslate"><spanclass="pre">tcplisten</span><spanclass="pre"><same</span><spanclass="pre">args></span></code> in another.</p>
starting it from a console or terminal (<codeclass="docutils literal notranslate"><spanclass="pre">python</span><spanclass="pre">-m</span><spanclass="pre">idlelib</span></code>) and see if
<p>On Unix-based systems with tcl/tk older than <codeclass="docutils literal notranslate"><spanclass="pre">8.6.11</span></code> (see
<codeclass="docutils literal notranslate"><spanclass="pre">About</span><spanclass="pre">IDLE</span></code>) certain characters of certain fonts can cause
a tk failure with a message to the terminal. This can happen either
if one starts IDLE to edit a file with such a character or later
when entering such a character. If one cannot upgrade tcl/tk,
then re-configure IDLE to use a font that works better.</p>
<p>By default, IDLE runs user code in a separate OS process rather than in
the user interface process that runs the shell and editor. In the execution
process, it replaces <codeclass="docutils literal notranslate"><spanclass="pre">sys.stdin</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">sys.stdout</span></code>, and <codeclass="docutils literal notranslate"><spanclass="pre">sys.stderr</span></code>
with objects that get input from and send output to the Shell window.
The original values stored in <codeclass="docutils literal notranslate"><spanclass="pre">sys.__stdin__</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">sys.__stdout__</span></code>, and
<codeclass="docutils literal notranslate"><spanclass="pre">sys.__stderr__</span></code> are not touched, but may be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p>
<p>Sending print output from one process to a text widget in another is
slower than printing to a system terminal in the same process.
This has the most effect when printing multiple arguments, as the string
for each argument, each separator, the newline are sent separately.
For development, this is usually not a problem, but if one wants to
print faster in IDLE, format and join together everything one wants
displayed together and then print a single string. Both format strings
and <aclass="reference internal"href="stdtypes.html#str.join"title="str.join"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">str.join()</span></code></a> can help combine fields and lines.</p>
created in the execution process, whether directly by user code or by
modules such as multiprocessing. If such subprocess use <codeclass="docutils literal notranslate"><spanclass="pre">input</span></code> from
sys.stdin or <codeclass="docutils literal notranslate"><spanclass="pre">print</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">write</span></code> to sys.stdout or sys.stderr,
IDLE should be started in a command line window. (On Windows,
use <codeclass="docutils literal notranslate"><spanclass="pre">python</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">py</span></code> rather than <codeclass="docutils literal notranslate"><spanclass="pre">pythonw</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">pyw</span></code>.)
will then be attached to that window for input and output.</p>
<p>If <codeclass="docutils literal notranslate"><spanclass="pre">sys</span></code> is reset by user code, such as with <codeclass="docutils literal notranslate"><spanclass="pre">importlib.reload(sys)</span></code>,
IDLE’s changes are lost and input from the keyboard and output to the screen
<p>A Tk Text widget, and hence IDLE’s Shell, displays characters (codepoints) in
the BMP (Basic Multilingual Plane) subset of Unicode. Which characters are
displayed with a proper glyph and which with a replacement box depends on the
operating system and installed fonts. Tab characters cause the following text
to begin after the next tab stop. (They occur every 8 ‘characters’). Newline
characters cause following text to appear on a new line. Other control
characters are ignored or displayed as a space, box, or something else,
depending on the operating system and font. (Moving the text cursor through
such output with arrow keys may exhibit some surprising spacing behavior.)</p>
<divclass="highlight-python3 notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>></span><spanclass="n">s</span><spanclass="o">=</span><spanclass="s1">'a</span><spanclass="se">\t</span><spanclass="s1">b</span><spanclass="se">\a</span><spanclass="s1"><</span><spanclass="se">\x02</span><spanclass="s1">><</span><spanclass="se">\r</span><spanclass="s1">></span><spanclass="se">\b</span><spanclass="s1">c</span><spanclass="se">\n</span><spanclass="s1">d'</span><spanclass="c1"># Enter 22 chars.</span>
<spanclass="gp">>>></span><spanclass="nb">print</span><spanclass="p">(</span><spanclass="n">s</span><spanclass="p">,</span><spanclass="n">end</span><spanclass="o">=</span><spanclass="s1">''</span><spanclass="p">)</span><spanclass="c1"># Display s as is.</span>
<spanclass="go"># Result varies by OS and font. Try it.</span>
facilitate development of tkinter programs. Enter <codeclass="docutils literal notranslate"><spanclass="pre">import</span><spanclass="pre">tkinter</span><spanclass="pre">as</span><spanclass="pre">tk;</span>
Next enter <codeclass="docutils literal notranslate"><spanclass="pre">b</span><spanclass="pre">=</span><spanclass="pre">tk.Button(root,</span><spanclass="pre">text='button');</span><spanclass="pre">b.pack()</span></code>. Again,
nothing visibly changes in standard Python until one enters <codeclass="docutils literal notranslate"><spanclass="pre">root.update()</span></code>.</p>
<p>Most tkinter programs run <codeclass="docutils literal notranslate"><spanclass="pre">root.mainloop()</span></code>, which usually does not
<codeclass="docutils literal notranslate"><spanclass="pre">python</span><spanclass="pre">-i</span></code> or from an IDLE editor, a <codeclass="docutils literal notranslate"><spanclass="pre">>>></span></code> shell prompt does not
appear until <codeclass="docutils literal notranslate"><spanclass="pre">mainloop()</span></code> returns, at which time there is nothing left
<spanid="idlelib-implementation-of-idle-application"></span><h2>idlelib — implementation of IDLE application<aclass="headerlink"href="#module-idlelib"title="Link to this heading">¶</a></h2>
sense that feature changes can be backported (see <spanclass="target"id="index-9"></span><aclass="pep reference external"href="https://peps.python.org/pep-0434/"><strong>PEP 434</strong></a>).</p>