| 1 |
<section id="idle"> |
| 2 |
<span id="idle-python-editor-and-shell"></span><h1>IDLE — Python editor and shell<a class="headerlink" href="#idle" title="Link to this heading">¶</a></h1> |
| 3 |
<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/idlelib/">Lib/idlelib/</a></p> |
| 4 |
<span class="target" id="index-0"></span><hr class="docutils" /> |
| 5 |
<p>IDLE is Python’s Integrated Development and Learning Environment.</p> |
| 6 |
<p>IDLE has the following features:</p> |
| 7 |
<ul class="simple"> |
| 8 |
<li><p>cross-platform: works mostly the same on Windows, Unix, and macOS</p></li> |
| 9 |
<li><p>Python shell window (interactive interpreter) with colorizing |
| 10 |
of code input, output, and error messages</p></li> |
| 11 |
<li><p>multi-window text editor with multiple undo, Python colorizing, |
| 12 |
smart indent, call tips, auto completion, and other features</p></li> |
| 13 |
<li><p>search within any window, replace within editor windows, and search |
| 14 |
through multiple files (grep)</p></li> |
| 15 |
<li><p>debugger with persistent breakpoints, stepping, and viewing |
| 16 |
of global and local namespaces</p></li> |
| 17 |
<li><p>configuration, browsers, and other dialogs</p></li> |
| 18 |
</ul> |
| 19 |
<p>The IDLE application is implemented in the <a class="reference internal" href="#module-idlelib" title="idlelib: Implementation package for the IDLE shell/editor."><code class="xref py py-mod docutils literal notranslate"><span class="pre">idlelib</span></code></a> package.</p> |
| 20 |
<p>This is an <a class="reference internal" href="../glossary.html#term-optional-module"><span class="xref std std-term">optional module</span></a>. |
| 21 |
If it is missing from your copy of CPython, |
| 22 |
look for documentation from your distributor (that is, |
| 23 |
whoever provided Python to you). |
| 24 |
If you are the distributor, see <a class="reference internal" href="../using/configure.html#optional-module-requirements"><span class="std std-ref">Requirements for optional modules</span></a>.</p> |
| 25 |
<section id="menus"> |
| 26 |
<h2>Menus<a class="headerlink" href="#menus" title="Link to this heading">¶</a></h2> |
| 27 |
<p>IDLE has two main window types, the Shell window and the Editor window. It is |
| 28 |
possible to have multiple editor windows simultaneously. On Windows and |
| 29 |
Linux, each has its own top menu. Each menu documented below indicates |
| 30 |
which window type it is associated with.</p> |
| 31 |
<p>Output windows, such as used for Edit => Find in Files, are a subtype of editor |
| 32 |
window. They currently have the same top menu but a different |
| 33 |
default title and context menu.</p> |
| 34 |
<p>On macOS, there is one application menu. It dynamically changes according |
| 35 |
to the window currently selected. It has an IDLE menu, and some entries |
| 36 |
described below are moved around to conform to Apple guidelines.</p> |
| 37 |
<section id="file-menu-shell-and-editor"> |
| 38 |
<h3>File menu (Shell and Editor)<a class="headerlink" href="#file-menu-shell-and-editor" title="Link to this heading">¶</a></h3> |
| 39 |
<dl class="simple"> |
| 40 |
<dt>New File</dt><dd><p>Create a new file editing window.</p> |
| 41 |
</dd> |
| 42 |
<dt>Open…</dt><dd><p>Open an existing file with an Open dialog.</p> |
| 43 |
</dd> |
| 44 |
<dt>Open Module…</dt><dd><p>Open an existing module (searches sys.path).</p> |
| 45 |
</dd> |
| 46 |
<dt>Recent Files</dt><dd><p>Open a list of recent files. Click one to open it.</p> |
| 47 |
</dd> |
| 48 |
</dl> |
| 49 |
<dl class="simple" id="index-1"> |
| 50 |
<dt>Module Browser</dt><dd><p>Show functions, classes, and methods in the current Editor file in a |
| 51 |
tree structure. In the shell, open a module first.</p> |
| 52 |
</dd> |
| 53 |
<dt>Path Browser</dt><dd><p>Show sys.path directories, modules, functions, classes and methods in a |
| 54 |
tree structure.</p> |
| 55 |
</dd> |
| 56 |
<dt>Save</dt><dd><p>Save the current window to the associated file, if there is one. Windows |
| 57 |
that have been changed since being opened or last saved have a * before |
| 58 |
and after the window title. If there is no associated file, |
| 59 |
do Save As instead.</p> |
| 60 |
</dd> |
| 61 |
<dt>Save As…</dt><dd><p>Save the current window with a Save As dialog. The file saved becomes the |
| 62 |
new associated file for the window. (If your file manager is set to hide |
| 63 |
extensions, the current extension will be omitted in the file name box. |
| 64 |
If the new filename has no ‘.’, ‘.py’ and ‘.txt’ will be added for Python |
| 65 |
and text files, except that on macOS Aqua,’.py’ is added for all files.)</p> |
| 66 |
</dd> |
| 67 |
<dt>Save Copy As…</dt><dd><p>Save the current window to different file without changing the associated |
| 68 |
file. (See Save As note above about filename extensions.)</p> |
| 69 |
</dd> |
| 70 |
<dt>Print Window</dt><dd><p>Print the current window to the default printer.</p> |
| 71 |
</dd> |
| 72 |
<dt>Close Window</dt><dd><p>Close the current window (if an unsaved editor, ask to save; if an unsaved |
| 73 |
Shell, ask to quit execution). Calling <code class="docutils literal notranslate"><span class="pre">exit()</span></code> or <code class="docutils literal notranslate"><span class="pre">close()</span></code> in the Shell |
| 74 |
window also closes Shell. If this is the only window, also exit IDLE.</p> |
| 75 |
</dd> |
| 76 |
<dt>Exit IDLE</dt><dd><p>Close all windows and quit IDLE (ask to save unsaved edit windows).</p> |
| 77 |
</dd> |
| 78 |
</dl> |
| 79 |
</section> |
| 80 |
<section id="edit-menu-shell-and-editor"> |
| 81 |
<h3>Edit menu (Shell and Editor)<a class="headerlink" href="#edit-menu-shell-and-editor" title="Link to this heading">¶</a></h3> |
| 82 |
<dl class="simple"> |
| 83 |
<dt>Undo</dt><dd><p>Undo the last change to the current window. A maximum of 1000 changes may |
| 84 |
be undone.</p> |
| 85 |
</dd> |
| 86 |
<dt>Redo</dt><dd><p>Redo the last undone change to the current window.</p> |
| 87 |
</dd> |
| 88 |
<dt>Select All</dt><dd><p>Select the entire contents of the current window.</p> |
| 89 |
</dd> |
| 90 |
<dt>Cut</dt><dd><p>Copy selection into the system-wide clipboard; then delete the selection.</p> |
| 91 |
</dd> |
| 92 |
<dt>Copy</dt><dd><p>Copy selection into the system-wide clipboard.</p> |
| 93 |
</dd> |
| 94 |
<dt>Paste</dt><dd><p>Insert contents of the system-wide clipboard into the current window.</p> |
| 95 |
</dd> |
| 96 |
</dl> |
| 97 |
<p>The clipboard functions are also available in context menus.</p> |
| 98 |
<dl class="simple"> |
| 99 |
<dt>Find…</dt><dd><p>Open a search dialog with many options</p> |
| 100 |
</dd> |
| 101 |
<dt>Find Again</dt><dd><p>Repeat the last search, if there is one.</p> |
| 102 |
</dd> |
| 103 |
<dt>Find Selection</dt><dd><p>Search for the currently selected string, if there is one.</p> |
| 104 |
</dd> |
| 105 |
<dt>Find in Files…</dt><dd><p>Open a file search dialog. Put results in a new output window.</p> |
| 106 |
</dd> |
| 107 |
<dt>Replace…</dt><dd><p>Open a search-and-replace dialog.</p> |
| 108 |
</dd> |
| 109 |
<dt>Go to Line</dt><dd><p>Move the cursor to the beginning of the line requested and make that |
| 110 |
line visible. A request past the end of the file goes to the end. |
| 111 |
Clear any selection and update the line and column status.</p> |
| 112 |
</dd> |
| 113 |
<dt>Show Completions</dt><dd><p>Open a scrollable list allowing selection of existing names. See |
| 114 |
<a class="reference internal" href="#completions"><span class="std std-ref">Completions</span></a> in the Editing and Navigation section below.</p> |
| 115 |
</dd> |
| 116 |
<dt>Expand Word</dt><dd><p>Expand a prefix you have typed to match a full word in the same window; |
| 117 |
repeat to get a different expansion.</p> |
| 118 |
</dd> |
| 119 |
<dt>Show Call Tip</dt><dd><p>After an unclosed parenthesis for a function, open a small window with |
| 120 |
function parameter hints. See <a class="reference internal" href="#calltips"><span class="std std-ref">Calltips</span></a> in the |
| 121 |
Editing and Navigation section below.</p> |
| 122 |
</dd> |
| 123 |
<dt>Show Surrounding Parens</dt><dd><p>Highlight the surrounding parenthesis.</p> |
| 124 |
</dd> |
| 125 |
</dl> |
| 126 |
</section> |
| 127 |
<section id="format-menu-editor-window-only"> |
| 128 |
<span id="format-menu"></span><h3>Format menu (Editor window only)<a class="headerlink" href="#format-menu-editor-window-only" title="Link to this heading">¶</a></h3> |
| 129 |
<dl class="simple"> |
| 130 |
<dt>Format Paragraph</dt><dd><p>Rewrap the text block containing the text insert cursor. |
| 131 |
Avoid code lines. See <a class="reference internal" href="#format-block"><span class="std std-ref">Format block</span></a> in the |
| 132 |
Editing and Navigation section below.</p> |
| 133 |
</dd> |
| 134 |
<dt>Indent Region</dt><dd><p>Shift selected lines right by the indent width (default 4 spaces).</p> |
| 135 |
</dd> |
| 136 |
<dt>Dedent Region</dt><dd><p>Shift selected lines left by the indent width (default 4 spaces).</p> |
| 137 |
</dd> |
| 138 |
<dt>Comment Out Region</dt><dd><p>Insert ## in front of selected lines.</p> |
| 139 |
</dd> |
| 140 |
<dt>Uncomment Region</dt><dd><p>Remove leading # or ## from selected lines.</p> |
| 141 |
</dd> |
| 142 |
<dt>Tabify Region</dt><dd><p>Turn <em>leading</em> stretches of spaces into tabs. (Note: We recommend using |
| 143 |
4 space blocks to indent Python code.)</p> |
| 144 |
</dd> |
| 145 |
<dt>Untabify Region</dt><dd><p>Turn <em>all</em> tabs into the correct number of spaces.</p> |
| 146 |
</dd> |
| 147 |
<dt>Toggle Tabs</dt><dd><p>Open a dialog to switch between indenting with spaces and tabs.</p> |
| 148 |
</dd> |
| 149 |
<dt>New Indent Width</dt><dd><p>Open a dialog to change indent width. The accepted default by the Python |
| 150 |
community is 4 spaces.</p> |
| 151 |
</dd> |
| 152 |
<dt>Strip Trailing Whitespace</dt><dd><p>Remove trailing space and other whitespace characters after the last |
| 153 |
non-whitespace character of a line by applying <a class="reference internal" href="stdtypes.html#str.rstrip" title="str.rstrip"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.rstrip()</span></code></a> to each line, |
| 154 |
including lines within multiline strings. Except for Shell windows, |
| 155 |
remove extra newlines at the end of the file.</p> |
| 156 |
</dd> |
| 157 |
</dl> |
| 158 |
</section> |
| 159 |
<section id="run-menu-editor-window-only"> |
| 160 |
<span id="index-2"></span><h3>Run menu (Editor window only)<a class="headerlink" href="#run-menu-editor-window-only" title="Link to this heading">¶</a></h3> |
| 161 |
<dl class="simple" id="run-module"> |
| 162 |
<dt>Run Module</dt><dd><p>Do <a class="reference internal" href="#check-module"><span class="std std-ref">Check Module</span></a>. If no error, restart the shell to clean the |
| 163 |
environment, then execute the module. Output is displayed in the Shell |
| 164 |
window. Note that output requires use of <code class="docutils literal notranslate"><span class="pre">print</span></code> or <code class="docutils literal notranslate"><span class="pre">write</span></code>. |
| 165 |
When execution is complete, the Shell retains focus and displays a prompt. |
| 166 |
At this point, one may interactively explore the result of execution. |
| 167 |
This is similar to executing a file with <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-i</span> <span class="pre">file</span></code> at a command |
| 168 |
line.</p> |
| 169 |
</dd> |
| 170 |
</dl> |
| 171 |
<dl class="simple" id="run-custom"> |
| 172 |
<dt>Run… Customized</dt><dd><p>Same as <a class="reference internal" href="#run-module"><span class="std std-ref">Run Module</span></a>, but run the module with customized |
| 173 |
settings. <em>Command Line Arguments</em> extend <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> as if passed |
| 174 |
on a command line. The module can be run in the Shell without restarting.</p> |
| 175 |
</dd> |
| 176 |
</dl> |
| 177 |
<dl class="simple" id="check-module"> |
| 178 |
<dt>Check Module</dt><dd><p>Check the syntax of the module currently open in the Editor window. If the |
| 179 |
module has not been saved IDLE will either prompt the user to save or |
| 180 |
autosave, as selected in the General tab of the Idle Settings dialog. If |
| 181 |
there is a syntax error, the approximate location is indicated in the |
| 182 |
Editor window.</p> |
| 183 |
</dd> |
| 184 |
</dl> |
| 185 |
<dl class="simple" id="python-shell"> |
| 186 |
<dt>Python Shell</dt><dd><p>Open or wake up the Python Shell window.</p> |
| 187 |
</dd> |
| 188 |
</dl> |
| 189 |
</section> |
| 190 |
<section id="shell-menu-shell-window-only"> |
| 191 |
<h3>Shell menu (Shell window only)<a class="headerlink" href="#shell-menu-shell-window-only" title="Link to this heading">¶</a></h3> |
| 192 |
<dl class="simple"> |
| 193 |
<dt>View Last Restart</dt><dd><p>Scroll the shell window to the last Shell restart.</p> |
| 194 |
</dd> |
| 195 |
<dt>Restart Shell</dt><dd><p>Restart the shell to clean the environment and reset display and exception handling.</p> |
| 196 |
</dd> |
| 197 |
<dt>Previous History</dt><dd><p>Cycle through earlier commands in history which match the current entry.</p> |
| 198 |
</dd> |
| 199 |
<dt>Next History</dt><dd><p>Cycle through later commands in history which match the current entry.</p> |
| 200 |
</dd> |
| 201 |
<dt>Interrupt Execution</dt><dd><p>Stop a running program.</p> |
| 202 |
</dd> |
| 203 |
</dl> |
| 204 |
</section> |
| 205 |
<section id="debug-menu-shell-window-only"> |
| 206 |
<h3>Debug menu (Shell window only)<a class="headerlink" href="#debug-menu-shell-window-only" title="Link to this heading">¶</a></h3> |
| 207 |
<dl class="simple"> |
| 208 |
<dt>Go to File/Line</dt><dd><p>Look on the current line. with the cursor, and the line above for a filename |
| 209 |
and line number. If found, open the file if not already open, and show the |
| 210 |
line. Use this to view source lines referenced in an exception traceback |
| 211 |
and lines found by Find in Files. Also available in the context menu of |
| 212 |
the Shell window and Output windows.</p> |
| 213 |
</dd> |
| 214 |
</dl> |
| 215 |
<dl class="simple" id="index-3"> |
| 216 |
<dt>Debugger (toggle)</dt><dd><p>When activated, code entered in the Shell or run from an Editor will run |
| 217 |
under the debugger. In the Editor, breakpoints can be set with the context |
| 218 |
menu. This feature is still incomplete and somewhat experimental.</p> |
| 219 |
</dd> |
| 220 |
<dt>Stack Viewer</dt><dd><p>Show the stack traceback of the last exception in a tree widget, with |
| 221 |
access to locals and globals.</p> |
| 222 |
</dd> |
| 223 |
<dt>Auto-open Stack Viewer</dt><dd><p>Toggle automatically opening the stack viewer on an unhandled exception.</p> |
| 224 |
</dd> |
| 225 |
</dl> |
| 226 |
</section> |
| 227 |
<section id="options-menu-shell-and-editor"> |
| 228 |
<h3>Options menu (Shell and Editor)<a class="headerlink" href="#options-menu-shell-and-editor" title="Link to this heading">¶</a></h3> |
| 229 |
<dl class="simple"> |
| 230 |
<dt>Configure IDLE</dt><dd><p>Open a configuration dialog and change preferences for the following: |
| 231 |
fonts, indentation, keybindings, text color themes, startup windows and |
| 232 |
size, additional help sources, and extensions. On macOS, open the |
| 233 |
configuration dialog by selecting Preferences in the application |
| 234 |
menu. For more details, see |
| 235 |
<a class="reference internal" href="#preferences"><span class="std std-ref">Setting preferences</span></a> under Help and preferences.</p> |
| 236 |
</dd> |
| 237 |
</dl> |
| 238 |
<p>Most configuration options apply to all windows or all future windows. |
| 239 |
The option items below only apply to the active window.</p> |
| 240 |
<dl class="simple"> |
| 241 |
<dt>Show/Hide Code Context (Editor Window only)</dt><dd><p>Open a pane at the top of the edit window which shows the block context |
| 242 |
of the code which has scrolled above the top of the window. See |
| 243 |
<a class="reference internal" href="#code-context"><span class="std std-ref">Code Context</span></a> in the Editing and Navigation section |
| 244 |
below.</p> |
| 245 |
</dd> |
| 246 |
<dt>Show/Hide Line Numbers (Editor Window only)</dt><dd><p>Open a column to the left of the edit window which shows the number |
| 247 |
of each line of text. The default is off, which may be changed in the |
| 248 |
preferences (see <a class="reference internal" href="#preferences"><span class="std std-ref">Setting preferences</span></a>).</p> |
| 249 |
</dd> |
| 250 |
<dt>Zoom/Restore Height</dt><dd><p>Toggles the window between normal size and maximum height. The initial size |
| 251 |
defaults to 40 lines by 80 chars unless changed on the General tab of the |
| 252 |
Configure IDLE dialog. The maximum height for a screen is determined by |
| 253 |
momentarily maximizing a window the first time one is zoomed on the screen. |
| 254 |
Changing screen settings may invalidate the saved height. This toggle has |
| 255 |
no effect when a window is maximized.</p> |
| 256 |
</dd> |
| 257 |
</dl> |
| 258 |
</section> |
| 259 |
<section id="window-menu-shell-and-editor"> |
| 260 |
<h3>Window menu (Shell and Editor)<a class="headerlink" href="#window-menu-shell-and-editor" title="Link to this heading">¶</a></h3> |
| 261 |
<p>Lists the names of all open windows; select one to bring it to the foreground |
| 262 |
(deiconifying it if necessary).</p> |
| 263 |
</section> |
| 264 |
<section id="help-menu-shell-and-editor"> |
| 265 |
<h3>Help menu (Shell and Editor)<a class="headerlink" href="#help-menu-shell-and-editor" title="Link to this heading">¶</a></h3> |
| 266 |
<dl class="simple"> |
| 267 |
<dt>About IDLE</dt><dd><p>Display version, copyright, license, credits, and more.</p> |
| 268 |
</dd> |
| 269 |
<dt>IDLE Help</dt><dd><p>Display this IDLE document, detailing the menu options, basic editing and |
| 270 |
navigation, and other tips.</p> |
| 271 |
</dd> |
| 272 |
<dt>Python Docs</dt><dd><p>Access local Python documentation, if installed, or start a web browser |
| 273 |
and open docs.python.org showing the latest Python documentation.</p> |
| 274 |
</dd> |
| 275 |
<dt>Turtle Demo</dt><dd><p>Run the turtledemo module with example Python code and turtle drawings.</p> |
| 276 |
</dd> |
| 277 |
</dl> |
| 278 |
<p>Additional help sources may be added here with the Configure IDLE dialog under |
| 279 |
the General tab. See the <a class="reference internal" href="#help-sources"><span class="std std-ref">Help sources</span></a> subsection below |
| 280 |
for more on Help menu choices.</p> |
| 281 |
</section> |
| 282 |
<section id="context-menus"> |
| 283 |
<span id="index-4"></span><h3>Context menus<a class="headerlink" href="#context-menus" title="Link to this heading">¶</a></h3> |
| 284 |
<p>Open a context menu by right-clicking in a window (Control-click on macOS). |
| 285 |
Context menus have the standard clipboard functions also on the Edit menu.</p> |
| 286 |
<dl class="simple"> |
| 287 |
<dt>Cut</dt><dd><p>Copy selection into the system-wide clipboard; then delete the selection.</p> |
| 288 |
</dd> |
| 289 |
<dt>Copy</dt><dd><p>Copy selection into the system-wide clipboard.</p> |
| 290 |
</dd> |
| 291 |
<dt>Paste</dt><dd><p>Insert contents of the system-wide clipboard into the current window.</p> |
| 292 |
</dd> |
| 293 |
</dl> |
| 294 |
<p>Editor windows also have breakpoint functions. Lines with a breakpoint set are |
| 295 |
specially marked. Breakpoints only have an effect when running under the |
| 296 |
debugger. Breakpoints for a file are saved in the user’s <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code> |
| 297 |
directory.</p> |
| 298 |
<dl class="simple"> |
| 299 |
<dt>Set Breakpoint</dt><dd><p>Set a breakpoint on the current line.</p> |
| 300 |
</dd> |
| 301 |
<dt>Clear Breakpoint</dt><dd><p>Clear the breakpoint on that line.</p> |
| 302 |
</dd> |
| 303 |
</dl> |
| 304 |
<p>Shell and Output windows also have the following.</p> |
| 305 |
<dl class="simple"> |
| 306 |
<dt>Go to file/line</dt><dd><p>Same as in Debug menu.</p> |
| 307 |
</dd> |
| 308 |
</dl> |
| 309 |
<p>The Shell window also has an output squeezing facility explained in the <em>Python |
| 310 |
Shell window</em> subsection below.</p> |
| 311 |
<dl class="simple"> |
| 312 |
<dt>Squeeze</dt><dd><p>If the cursor is over an output line, squeeze all the output between |
| 313 |
the code above and the prompt below down to a ‘Squeezed text’ label.</p> |
| 314 |
</dd> |
| 315 |
</dl> |
| 316 |
</section> |
| 317 |
</section> |
| 318 |
<section id="editing-and-navigation"> |
| 319 |
<span id="id1"></span><h2>Editing and Navigation<a class="headerlink" href="#editing-and-navigation" title="Link to this heading">¶</a></h2> |
| 320 |
<section id="editor-windows"> |
| 321 |
<h3>Editor windows<a class="headerlink" href="#editor-windows" title="Link to this heading">¶</a></h3> |
| 322 |
<p>IDLE may open editor windows when it starts, depending on settings |
| 323 |
and how you start IDLE. Thereafter, use the File menu. There can be only |
| 324 |
one open editor window for a given file.</p> |
| 325 |
<p>The title bar contains the name of the file, the full path, and the version |
| 326 |
of Python and IDLE running the window. The status bar contains the line |
| 327 |
number (‘Ln’) and column number (‘Col’). Line numbers start with 1; |
| 328 |
column numbers with 0.</p> |
| 329 |
<p>IDLE assumes that files with a known .py* extension contain Python code |
| 330 |
and that other files do not. Run Python code with the Run menu.</p> |
| 331 |
</section> |
| 332 |
<section id="key-bindings"> |
| 333 |
<h3>Key bindings<a class="headerlink" href="#key-bindings" title="Link to this heading">¶</a></h3> |
| 334 |
<p>The IDLE insertion cursor is a thin vertical bar between character |
| 335 |
positions. When characters are entered, the insertion cursor and |
| 336 |
everything to its right moves right one character and |
| 337 |
the new character is entered in the new space.</p> |
| 338 |
<p>Several non-character keys move the cursor and possibly |
| 339 |
delete characters. Deletion does not puts text on the clipboard, |
| 340 |
but IDLE has an undo list. Wherever this doc discusses keys, |
| 341 |
‘C’ refers to the <kbd class="kbd docutils literal notranslate">Control</kbd> key on Windows and |
| 342 |
Unix and the <kbd class="kbd docutils literal notranslate">Command</kbd> key on macOS. (And all such discussions |
| 343 |
assume that the keys have not been re-bound to something else.)</p> |
| 344 |
<ul class="simple"> |
| 345 |
<li><p>Arrow keys move the cursor one character or line.</p></li> |
| 346 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">LeftArrow</kbd> and <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">RightArrow</kbd> moves left or right one word.</p></li> |
| 347 |
<li><p><kbd class="kbd docutils literal notranslate">Home</kbd> and <kbd class="kbd docutils literal notranslate">End</kbd> go to the beginning or end of the line.</p></li> |
| 348 |
<li><p><kbd class="kbd docutils literal notranslate">Page Up</kbd> and <kbd class="kbd docutils literal notranslate">Page Down</kbd> go up or down one screen.</p></li> |
| 349 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">Home</kbd> and <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">End</kbd> go to beginning or end of the file.</p></li> |
| 350 |
<li><p><kbd class="kbd docutils literal notranslate">Backspace</kbd> and <kbd class="kbd docutils literal notranslate">Del</kbd> (or <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">d</kbd>) delete the previous |
| 351 |
or next character.</p></li> |
| 352 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">Backspace</kbd> and <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">Del</kbd> delete one word left or right.</p></li> |
| 353 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">k</kbd> deletes (‘kills’) everything to the right.</p></li> |
| 354 |
</ul> |
| 355 |
<p>Standard keybindings (like <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">c</kbd> to copy and <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">v</kbd> to paste) |
| 356 |
may work. Keybindings are selected in the Configure IDLE dialog.</p> |
| 357 |
</section> |
| 358 |
<section id="automatic-indentation"> |
| 359 |
<h3>Automatic indentation<a class="headerlink" href="#automatic-indentation" title="Link to this heading">¶</a></h3> |
| 360 |
<p>After a block-opening statement, the next line is indented by 4 spaces (in the |
| 361 |
Python Shell window by one tab). After certain keywords (break, return etc.) |
| 362 |
the next line is dedented. In leading indentation, <kbd class="kbd docutils literal notranslate">Backspace</kbd> deletes up |
| 363 |
to 4 spaces if they are there. <kbd class="kbd docutils literal notranslate">Tab</kbd> inserts spaces (in the Python |
| 364 |
Shell window one tab), number depends on Indent width. Currently, tabs |
| 365 |
are restricted to four spaces due to Tcl/Tk limitations.</p> |
| 366 |
<p>See also the indent/dedent region commands on the |
| 367 |
<a class="reference internal" href="#format-menu"><span class="std std-ref">Format menu</span></a>.</p> |
| 368 |
</section> |
| 369 |
<section id="search-and-replace"> |
| 370 |
<h3>Search and Replace<a class="headerlink" href="#search-and-replace" title="Link to this heading">¶</a></h3> |
| 371 |
<p>Any selection becomes a search target. However, only selections within |
| 372 |
a line work because searches are only performed within lines with the |
| 373 |
terminal newline removed. If <code class="docutils literal notranslate"><span class="pre">[x]</span> <span class="pre">Regular</span> <span class="pre">expression</span></code> is checked, the |
| 374 |
target is interpreted according to the Python re module.</p> |
| 375 |
</section> |
| 376 |
<section id="completions"> |
| 377 |
<span id="id2"></span><h3>Completions<a class="headerlink" href="#completions" title="Link to this heading">¶</a></h3> |
| 378 |
<p>Completions are supplied, when requested and available, for module |
| 379 |
names, attributes of classes or functions, or filenames. Each request |
| 380 |
method displays a completion box with existing names. (See tab |
| 381 |
completions below for an exception.) For any box, change the name |
| 382 |
being completed and the item highlighted in the box by |
| 383 |
typing and deleting characters; by hitting <kbd class="kbd docutils literal notranslate">Up</kbd>, <kbd class="kbd docutils literal notranslate">Down</kbd>, |
| 384 |
<kbd class="kbd docutils literal notranslate">PageUp</kbd>, <kbd class="kbd docutils literal notranslate">PageDown</kbd>, <kbd class="kbd docutils literal notranslate">Home</kbd>, and <kbd class="kbd docutils literal notranslate">End</kbd> keys; |
| 385 |
and by a single click within the box. Close the box with <kbd class="kbd docutils literal notranslate">Escape</kbd>, |
| 386 |
<kbd class="kbd docutils literal notranslate">Enter</kbd>, and double <kbd class="kbd docutils literal notranslate">Tab</kbd> keys or clicks outside the box. |
| 387 |
A double click within the box selects and closes.</p> |
| 388 |
<p>One way to open a box is to type a key character and wait for a |
| 389 |
predefined interval. This defaults to 2 seconds; customize it |
| 390 |
in the settings dialog. (To prevent auto popups, set the delay to a |
| 391 |
large number of milliseconds, such as 100000000.) For imported module |
| 392 |
names or class or function attributes, type ‘.’. |
| 393 |
For filenames in the root directory, type <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a> or |
| 394 |
<a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.altsep</span></code></a> immediately after an opening quote. (On Windows, |
| 395 |
one can specify a drive first.) Move into subdirectories by typing a |
| 396 |
directory name and a separator.</p> |
| 397 |
<p>Instead of waiting, or after a box is closed, open a completion box |
| 398 |
immediately with Show Completions on the Edit menu. The default hot |
| 399 |
key is <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">space</kbd>. If one types a prefix for the desired name |
| 400 |
before opening the box, the first match or near miss is made visible. |
| 401 |
The result is the same as if one enters a prefix |
| 402 |
after the box is displayed. Show Completions after a quote completes |
| 403 |
filenames in the current directory instead of a root directory.</p> |
| 404 |
<p>Hitting <kbd class="kbd docutils literal notranslate">Tab</kbd> after a prefix usually has the same effect as Show |
| 405 |
Completions. (With no prefix, it indents.) However, if there is only |
| 406 |
one match to the prefix, that match is immediately added to the editor |
| 407 |
text without opening a box.</p> |
| 408 |
<p>Invoking ‘Show Completions’, or hitting <kbd class="kbd docutils literal notranslate">Tab</kbd> after a prefix, |
| 409 |
outside of a string and without a preceding ‘.’ opens a box with |
| 410 |
keywords, builtin names, and available module-level names.</p> |
| 411 |
<p>When editing code in an editor (as oppose to Shell), increase the |
| 412 |
available module-level names by running your code |
| 413 |
and not restarting the Shell thereafter. This is especially useful |
| 414 |
after adding imports at the top of a file. This also increases |
| 415 |
possible attribute completions.</p> |
| 416 |
<p>Completion boxes initially exclude names beginning with ‘_’ or, for |
| 417 |
modules, not included in ‘__all__’. The hidden names can be accessed |
| 418 |
by typing ‘_’ after ‘.’, either before or after the box is opened.</p> |
| 419 |
</section> |
| 420 |
<section id="calltips"> |
| 421 |
<span id="id3"></span><h3>Calltips<a class="headerlink" href="#calltips" title="Link to this heading">¶</a></h3> |
| 422 |
<p>A calltip is shown automatically when one types <kbd class="kbd docutils literal notranslate">(</kbd> after the name |
| 423 |
of an <em>accessible</em> function. A function name expression may include |
| 424 |
dots and subscripts. A calltip remains until it is clicked, the cursor |
| 425 |
is moved out of the argument area, or <kbd class="kbd docutils literal notranslate">)</kbd> is typed. Whenever the |
| 426 |
cursor is in the argument part of a definition, select Edit and “Show |
| 427 |
Call Tip” on the menu or enter its shortcut to display a calltip.</p> |
| 428 |
<p>The calltip consists of the function’s signature and docstring up to |
| 429 |
the latter’s first blank line or the fifth non-blank line. (Some builtin |
| 430 |
functions lack an accessible signature.) A ‘/’ or ‘*’ in the signature |
| 431 |
indicates that the preceding or following arguments are passed by |
| 432 |
position or name (keyword) only. Details are subject to change.</p> |
| 433 |
<p>In Shell, the accessible functions depends on what modules have been |
| 434 |
imported into the user process, including those imported by Idle itself, |
| 435 |
and which definitions have been run, all since the last restart.</p> |
| 436 |
<p>For example, restart the Shell and enter <code class="docutils literal notranslate"><span class="pre">itertools.count(</span></code>. A calltip |
| 437 |
appears because Idle imports itertools into the user process for its own |
| 438 |
use. (This could change.) Enter <code class="docutils literal notranslate"><span class="pre">turtle.write(</span></code> and nothing appears. |
| 439 |
Idle does not itself import turtle. The menu entry and shortcut also do |
| 440 |
nothing. Enter <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">turtle</span></code>. Thereafter, <code class="docutils literal notranslate"><span class="pre">turtle.write(</span></code> |
| 441 |
will display a calltip.</p> |
| 442 |
<p>In an editor, import statements have no effect until one runs the file. |
| 443 |
One might want to run a file after writing import statements, after |
| 444 |
adding function definitions, or after opening an existing file.</p> |
| 445 |
</section> |
| 446 |
<section id="format-block"> |
| 447 |
<span id="id4"></span><h3>Format block<a class="headerlink" href="#format-block" title="Link to this heading">¶</a></h3> |
| 448 |
<p>Reformat Paragraph rewraps a block (‘paragraph’) of contiguous equally |
| 449 |
indented non-blank comments, a similar block of text within a multiline |
| 450 |
string, or a selected subset of either. |
| 451 |
If needed, add a blank line to separate string from code. |
| 452 |
Partial lines in a selection expand to complete lines. |
| 453 |
The resulting lines have the same indent as before |
| 454 |
but have maximum total length of N columns (characters). |
| 455 |
Change the default N of 72 on the Window tab of IDLE Settings.</p> |
| 456 |
</section> |
| 457 |
<section id="code-context"> |
| 458 |
<span id="id5"></span><h3>Code Context<a class="headerlink" href="#code-context" title="Link to this heading">¶</a></h3> |
| 459 |
<p>Within an editor window containing Python code, code context can be toggled |
| 460 |
in order to show or hide a pane at the top of the window. When shown, this |
| 461 |
pane freezes the opening lines for block code, such as those beginning with |
| 462 |
<code class="docutils literal notranslate"><span class="pre">class</span></code>, <code class="docutils literal notranslate"><span class="pre">def</span></code>, or <code class="docutils literal notranslate"><span class="pre">if</span></code> keywords, that would have otherwise scrolled |
| 463 |
out of view. The size of the pane will be expanded and contracted as needed |
| 464 |
to show the all current levels of context, up to the maximum number of |
| 465 |
lines defined in the Configure IDLE dialog (which defaults to 15). If there |
| 466 |
are no current context lines and the feature is toggled on, a single blank |
| 467 |
line will display. Clicking on a line in the context pane will move that |
| 468 |
line to the top of the editor.</p> |
| 469 |
<p>The text and background colors for the context pane can be configured under |
| 470 |
the Highlights tab in the Configure IDLE dialog.</p> |
| 471 |
</section> |
| 472 |
<section id="shell-window"> |
| 473 |
<h3>Shell window<a class="headerlink" href="#shell-window" title="Link to this heading">¶</a></h3> |
| 474 |
<p>In IDLE’s Shell, enter, edit, and recall complete statements. (Most |
| 475 |
consoles and terminals only work with a single physical line at a time).</p> |
| 476 |
<p>Submit a single-line statement for execution by hitting <kbd class="kbd docutils literal notranslate">Return</kbd> |
| 477 |
with the cursor anywhere on the line. If a line is extended with |
| 478 |
Backslash (<kbd class="kbd docutils literal notranslate">\</kbd>), the cursor must be on the last physical line. |
| 479 |
Submit a multi-line compound statement by entering a blank line after |
| 480 |
the statement.</p> |
| 481 |
<p>When one pastes code into Shell, it is not compiled and possibly executed |
| 482 |
until one hits <kbd class="kbd docutils literal notranslate">Return</kbd>, as specified above. |
| 483 |
One may edit pasted code first. |
| 484 |
If one pastes more than one statement into Shell, the result will be a |
| 485 |
<a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> when multiple statements are compiled as if they were one.</p> |
| 486 |
<p>Lines containing <code class="docutils literal notranslate"><span class="pre">RESTART</span></code> mean that the user execution process has been |
| 487 |
re-started. This occurs when the user execution process has crashed, |
| 488 |
when one requests a restart on the Shell menu, or when one runs code |
| 489 |
in an editor window.</p> |
| 490 |
<p>The editing features described in previous subsections work when entering |
| 491 |
code interactively. IDLE’s Shell window also responds to the following:</p> |
| 492 |
<ul class="simple"> |
| 493 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">c</kbd> attempts to interrupt statement execution (but may fail).</p></li> |
| 494 |
<li><p><kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">d</kbd> closes Shell if typed at a <code class="docutils literal notranslate"><span class="pre">>>></span></code> prompt.</p></li> |
| 495 |
<li><p><kbd class="kbd docutils literal notranslate">Alt</kbd>-<kbd class="kbd docutils literal notranslate">p</kbd> and <kbd class="kbd docutils literal notranslate">Alt</kbd>-<kbd class="kbd docutils literal notranslate">n</kbd> (<kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">p</kbd> and <kbd class="kbd docutils literal notranslate">C</kbd>-<kbd class="kbd docutils literal notranslate">n</kbd> on macOS) |
| 496 |
retrieve to the current prompt the previous or next previously |
| 497 |
entered statement that matches anything already typed.</p></li> |
| 498 |
<li><p><kbd class="kbd docutils literal notranslate">Return</kbd> while the cursor is on any previous statement |
| 499 |
appends the latter to anything already typed at the prompt.</p></li> |
| 500 |
</ul> |
| 501 |
</section> |
| 502 |
<section id="text-colors"> |
| 503 |
<h3>Text colors<a class="headerlink" href="#text-colors" title="Link to this heading">¶</a></h3> |
| 504 |
<p>Idle defaults to black on white text, but colors text with special meanings. |
| 505 |
For the shell, these are shell output, shell error, user output, and |
| 506 |
user error. For Python code, at the shell prompt or in an editor, these are |
| 507 |
keywords, builtin class and function names, names following <code class="docutils literal notranslate"><span class="pre">class</span></code> and |
| 508 |
<code class="docutils literal notranslate"><span class="pre">def</span></code>, strings, and comments. For any text window, these are the cursor (when |
| 509 |
present), found text (when possible), and selected text.</p> |
| 510 |
<p>IDLE also highlights the <a class="reference internal" href="../reference/lexical_analysis.html#soft-keywords"><span class="std std-ref">soft keywords</span></a> <a class="reference internal" href="../reference/compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a>, |
| 511 |
<a class="reference internal" href="../reference/compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">case</span></code></a>, and <a class="reference internal" href="../reference/compound_stmts.html#wildcard-patterns"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">_</span></code></a> in |
| 512 |
pattern-matching statements. However, this highlighting is not perfect and |
| 513 |
will be incorrect in some rare cases, including some <code class="docutils literal notranslate"><span class="pre">_</span></code>-s in <code class="docutils literal notranslate"><span class="pre">case</span></code> |
| 514 |
patterns.</p> |
| 515 |
<p>Text coloring is done in the background, so uncolorized text is occasionally |
| 516 |
visible. To change the color scheme, use the Configure IDLE dialog |
| 517 |
Highlighting tab. The marking of debugger breakpoint lines in the editor and |
| 518 |
text in popups and dialogs is not user-configurable.</p> |
| 519 |
</section> |
| 520 |
</section> |
| 521 |
<section id="startup-and-code-execution"> |
| 522 |
<h2>Startup and Code Execution<a class="headerlink" href="#startup-and-code-execution" title="Link to this heading">¶</a></h2> |
| 523 |
<p>Upon startup with the <code class="docutils literal notranslate"><span class="pre">-s</span></code> option, IDLE will execute the file referenced by |
| 524 |
the environment variables <span class="target" id="index-5"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> or <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a>. |
| 525 |
IDLE first checks for <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code>; if <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> is present the file |
| 526 |
referenced is run. If <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> is not present, IDLE checks for |
| 527 |
<code class="docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code>. Files referenced by these environment variables are |
| 528 |
convenient places to store functions that are used frequently from the IDLE |
| 529 |
shell, or for executing import statements to import common modules.</p> |
| 530 |
<p>In addition, <code class="docutils literal notranslate"><span class="pre">Tk</span></code> also loads a startup file if it is present. Note that the |
| 531 |
Tk file is loaded unconditionally. This additional file is <code class="docutils literal notranslate"><span class="pre">.Idle.py</span></code> and is |
| 532 |
looked for in the user’s home directory. Statements in this file will be |
| 533 |
executed in the Tk namespace, so this file is not useful for importing |
| 534 |
functions to be used from IDLE’s Python shell.</p> |
| 535 |
<section id="command-line-usage"> |
| 536 |
<span id="idlelib-cli"></span><h3>Command-line usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h3> |
| 537 |
<p>IDLE can be invoked from the command line with various options. The general syntax is:</p> |
| 538 |
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>idlelib<span class="w"> </span><span class="o">[</span>options<span class="o">]</span><span class="w"> </span><span class="o">[</span>file<span class="w"> </span>...<span class="o">]</span> |
| 539 |
</pre></div> |
| 540 |
</div> |
| 541 |
<p>The following options are available:</p> |
| 542 |
<dl class="std option"> |
| 543 |
<dt class="sig sig-object std" id="cmdoption-idle-c"> |
| 544 |
<span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"> <span class="pre"><command></span></span><a class="headerlink" href="#cmdoption-idle-c" title="Link to this definition">¶</a></dt> |
| 545 |
<dd><p>Run the specified Python command in the shell window. |
| 546 |
For example, pass <code class="docutils literal notranslate"><span class="pre">-c</span> <span class="pre">"print('Hello,</span> <span class="pre">World!')"</span></code>. |
| 547 |
On Windows, the outer quotes must be double quotes as shown.</p> |
| 548 |
</dd></dl> |
| 549 |
|
| 550 |
<dl class="std option"> |
| 551 |
<dt class="sig sig-object std" id="cmdoption-idle-d"> |
| 552 |
<span class="sig-name descname"><span class="pre">-d</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-d" title="Link to this definition">¶</a></dt> |
| 553 |
<dd><p>Enable the debugger and open the shell window.</p> |
| 554 |
</dd></dl> |
| 555 |
|
| 556 |
<dl class="std option"> |
| 557 |
<dt class="sig sig-object std" id="cmdoption-idle-e"> |
| 558 |
<span class="sig-name descname"><span class="pre">-e</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-e" title="Link to this definition">¶</a></dt> |
| 559 |
<dd><p>Open an editor window.</p> |
| 560 |
</dd></dl> |
| 561 |
|
| 562 |
<dl class="std option"> |
| 563 |
<dt class="sig sig-object std" id="cmdoption-idle-h"> |
| 564 |
<span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-h" title="Link to this definition">¶</a></dt> |
| 565 |
<dd><p>Print a help message with legal combinations of options and exit.</p> |
| 566 |
</dd></dl> |
| 567 |
|
| 568 |
<dl class="std option"> |
| 569 |
<dt class="sig sig-object std" id="cmdoption-idle-i"> |
| 570 |
<span class="sig-name descname"><span class="pre">-i</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-i" title="Link to this definition">¶</a></dt> |
| 571 |
<dd><p>Open a shell window.</p> |
| 572 |
</dd></dl> |
| 573 |
|
| 574 |
<dl class="std option"> |
| 575 |
<dt class="sig sig-object std" id="cmdoption-idle-r"> |
| 576 |
<span class="sig-name descname"><span class="pre">-r</span></span><span class="sig-prename descclassname"> <span class="pre"><file></span></span><a class="headerlink" href="#cmdoption-idle-r" title="Link to this definition">¶</a></dt> |
| 577 |
<dd><p>Run the specified file in the shell window.</p> |
| 578 |
</dd></dl> |
| 579 |
|
| 580 |
<dl class="std option"> |
| 581 |
<dt class="sig sig-object std" id="cmdoption-idle-s"> |
| 582 |
<span class="sig-name descname"><span class="pre">-s</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-s" title="Link to this definition">¶</a></dt> |
| 583 |
<dd><p>Run the startup file (as defined by the environment variables <span class="target" id="index-7"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> or <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a>) before opening the shell window.</p> |
| 584 |
</dd></dl> |
| 585 |
|
| 586 |
<dl class="std option"> |
| 587 |
<dt class="sig sig-object std" id="cmdoption-idle-t"> |
| 588 |
<span class="sig-name descname"><span class="pre">-t</span></span><span class="sig-prename descclassname"> <span class="pre"><title></span></span><a class="headerlink" href="#cmdoption-idle-t" title="Link to this definition">¶</a></dt> |
| 589 |
<dd><p>Set the title of the shell window.</p> |
| 590 |
</dd></dl> |
| 591 |
|
| 592 |
<dl class="std option"> |
| 593 |
<dt class="sig sig-object std" id="cmdoption-idle-0"> |
| 594 |
<span class="sig-name descname"><span class="pre">-</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-idle-0" title="Link to this definition">¶</a></dt> |
| 595 |
<dd><p>Read and execute standard input in the shell window. This option must be the last one before any arguments.</p> |
| 596 |
</dd></dl> |
| 597 |
|
| 598 |
<p>If arguments are provided:</p> |
| 599 |
<ul class="simple"> |
| 600 |
<li><p>If <code class="docutils literal notranslate"><span class="pre">-</span></code>, <code class="docutils literal notranslate"><span class="pre">-c</span></code>, or <code class="docutils literal notranslate"><span class="pre">-r</span></code> is used, all arguments are placed in <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>, |
| 601 |
and <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> is set to <code class="docutils literal notranslate"><span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">'-c'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'-r'</span></code> respectively. |
| 602 |
No editor window is opened, even if that is the default set in the <em>Options</em> dialog.</p></li> |
| 603 |
<li><p>Otherwise, arguments are treated as files to be opened for editing, and <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> reflects the arguments passed to IDLE itself.</p></li> |
| 604 |
</ul> |
| 605 |
</section> |
| 606 |
<section id="startup-failure"> |
| 607 |
<h3>Startup failure<a class="headerlink" href="#startup-failure" title="Link to this heading">¶</a></h3> |
| 608 |
<p>IDLE uses a socket to communicate between the IDLE GUI process and the user |
| 609 |
code execution process. A connection must be established whenever the Shell |
| 610 |
starts or restarts. (The latter is indicated by a divider line that says |
| 611 |
‘RESTART’). If the user process fails to connect to the GUI process, it |
| 612 |
usually displays a <code class="docutils literal notranslate"><span class="pre">Tk</span></code> error box with a ‘cannot connect’ message |
| 613 |
that directs the user here. It then exits.</p> |
| 614 |
<p>One specific connection failure on Unix systems results from |
| 615 |
misconfigured masquerading rules somewhere in a system’s network setup. |
| 616 |
When IDLE is started from a terminal, one will see a message starting |
| 617 |
with <code class="docutils literal notranslate"><span class="pre">**</span> <span class="pre">Invalid</span> <span class="pre">host:</span></code>. |
| 618 |
The valid value is <code class="docutils literal notranslate"><span class="pre">127.0.0.1</span> <span class="pre">(idlelib.rpc.LOCALHOST)</span></code>. |
| 619 |
One can diagnose with <code class="docutils literal notranslate"><span class="pre">tcpconnect</span> <span class="pre">-irv</span> <span class="pre">127.0.0.1</span> <span class="pre">6543</span></code> in one |
| 620 |
terminal window and <code class="docutils literal notranslate"><span class="pre">tcplisten</span> <span class="pre"><same</span> <span class="pre">args></span></code> in another.</p> |
| 621 |
<p>A common cause of failure is a user-written file with the same name as a |
| 622 |
standard library module, such as <em>random.py</em> and <em>tkinter.py</em>. When such a |
| 623 |
file is located in the same directory as a file that is about to be run, |
| 624 |
IDLE cannot import the stdlib file. The current fix is to rename the |
| 625 |
user file.</p> |
| 626 |
<p>Though less common than in the past, an antivirus or firewall program may |
| 627 |
stop the connection. If the program cannot be taught to allow the |
| 628 |
connection, then it must be turned off for IDLE to work. It is safe to |
| 629 |
allow this internal connection because no data is visible on external |
| 630 |
ports. A similar problem is a network mis-configuration that blocks |
| 631 |
connections.</p> |
| 632 |
<p>Python installation issues occasionally stop IDLE: multiple versions can |
| 633 |
clash, or a single installation might need admin access. If one undo the |
| 634 |
clash, or cannot or does not want to run as admin, it might be easiest to |
| 635 |
completely remove Python and start over.</p> |
| 636 |
<p>A zombie pythonw.exe process could be a problem. On Windows, use Task |
| 637 |
Manager to check for one and stop it if there is. Sometimes a restart |
| 638 |
initiated by a program crash or Keyboard Interrupt (control-C) may fail |
| 639 |
to connect. Dismissing the error box or using Restart Shell on the Shell |
| 640 |
menu may fix a temporary problem.</p> |
| 641 |
<p>When IDLE first starts, it attempts to read user configuration files in |
| 642 |
<code class="docutils literal notranslate"><span class="pre">~/.idlerc/</span></code> (~ is one’s home directory). If there is a problem, an error |
| 643 |
message should be displayed. Leaving aside random disk glitches, this can |
| 644 |
be prevented by never editing the files by hand. Instead, use the |
| 645 |
configuration dialog, under Options. Once there is an error in a user |
| 646 |
configuration file, the best solution may be to delete it and start over |
| 647 |
with the settings dialog.</p> |
| 648 |
<p>If IDLE quits with no message, and it was not started from a console, try |
| 649 |
starting it from a console or terminal (<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">idlelib</span></code>) and see if |
| 650 |
this results in an error message.</p> |
| 651 |
<p>On Unix-based systems with tcl/tk older than <code class="docutils literal notranslate"><span class="pre">8.6.11</span></code> (see |
| 652 |
<code class="docutils literal notranslate"><span class="pre">About</span> <span class="pre">IDLE</span></code>) certain characters of certain fonts can cause |
| 653 |
a tk failure with a message to the terminal. This can happen either |
| 654 |
if one starts IDLE to edit a file with such a character or later |
| 655 |
when entering such a character. If one cannot upgrade tcl/tk, |
| 656 |
then re-configure IDLE to use a font that works better.</p> |
| 657 |
</section> |
| 658 |
<section id="running-user-code"> |
| 659 |
<h3>Running user code<a class="headerlink" href="#running-user-code" title="Link to this heading">¶</a></h3> |
| 660 |
<p>With rare exceptions, the result of executing Python code with IDLE is |
| 661 |
intended to be the same as executing the same code by the default method, |
| 662 |
directly with Python in a text-mode system console or terminal window. |
| 663 |
However, the different interface and operation occasionally affect |
| 664 |
visible results. For instance, <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> starts with more entries, |
| 665 |
and <code class="docutils literal notranslate"><span class="pre">threading.active_count()</span></code> returns 2 instead of 1.</p> |
| 666 |
<p>By default, IDLE runs user code in a separate OS process rather than in |
| 667 |
the user interface process that runs the shell and editor. In the execution |
| 668 |
process, it replaces <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>, and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> |
| 669 |
with objects that get input from and send output to the Shell window. |
| 670 |
The original values stored in <code class="docutils literal notranslate"><span class="pre">sys.__stdin__</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.__stdout__</span></code>, and |
| 671 |
<code class="docutils literal notranslate"><span class="pre">sys.__stderr__</span></code> are not touched, but may be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> |
| 672 |
<p>Sending print output from one process to a text widget in another is |
| 673 |
slower than printing to a system terminal in the same process. |
| 674 |
This has the most effect when printing multiple arguments, as the string |
| 675 |
for each argument, each separator, the newline are sent separately. |
| 676 |
For development, this is usually not a problem, but if one wants to |
| 677 |
print faster in IDLE, format and join together everything one wants |
| 678 |
displayed together and then print a single string. Both format strings |
| 679 |
and <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a> can help combine fields and lines.</p> |
| 680 |
<p>IDLE’s standard stream replacements are not inherited by subprocesses |
| 681 |
created in the execution process, whether directly by user code or by |
| 682 |
modules such as multiprocessing. If such subprocess use <code class="docutils literal notranslate"><span class="pre">input</span></code> from |
| 683 |
sys.stdin or <code class="docutils literal notranslate"><span class="pre">print</span></code> or <code class="docutils literal notranslate"><span class="pre">write</span></code> to sys.stdout or sys.stderr, |
| 684 |
IDLE should be started in a command line window. (On Windows, |
| 685 |
use <code class="docutils literal notranslate"><span class="pre">python</span></code> or <code class="docutils literal notranslate"><span class="pre">py</span></code> rather than <code class="docutils literal notranslate"><span class="pre">pythonw</span></code> or <code class="docutils literal notranslate"><span class="pre">pyw</span></code>.) |
| 686 |
The secondary subprocess |
| 687 |
will then be attached to that window for input and output.</p> |
| 688 |
<p>If <code class="docutils literal notranslate"><span class="pre">sys</span></code> is reset by user code, such as with <code class="docutils literal notranslate"><span class="pre">importlib.reload(sys)</span></code>, |
| 689 |
IDLE’s changes are lost and input from the keyboard and output to the screen |
| 690 |
will not work correctly.</p> |
| 691 |
<p>When Shell has the focus, it controls the keyboard and screen. This is |
| 692 |
normally transparent, but functions that directly access the keyboard |
| 693 |
and screen will not work. These include system-specific functions that |
| 694 |
determine whether a key has been pressed and if so, which.</p> |
| 695 |
<p>The IDLE code running in the execution process adds frames to the call stack |
| 696 |
that would not be there otherwise. IDLE wraps <code class="docutils literal notranslate"><span class="pre">sys.getrecursionlimit</span></code> and |
| 697 |
<code class="docutils literal notranslate"><span class="pre">sys.setrecursionlimit</span></code> to reduce the effect of the additional stack |
| 698 |
frames.</p> |
| 699 |
<p>When user code raises SystemExit either directly or by calling sys.exit, |
| 700 |
IDLE returns to a Shell prompt instead of exiting.</p> |
| 701 |
</section> |
| 702 |
<section id="user-output-in-shell"> |
| 703 |
<h3>User output in Shell<a class="headerlink" href="#user-output-in-shell" title="Link to this heading">¶</a></h3> |
| 704 |
<p>When a program outputs text, the result is determined by the |
| 705 |
corresponding output device. When IDLE executes user code, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> |
| 706 |
and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> are connected to the display area of IDLE’s Shell. Some of |
| 707 |
its features are inherited from the underlying Tk Text widget. Others |
| 708 |
are programmed additions. Where it matters, Shell is designed for development |
| 709 |
rather than production runs.</p> |
| 710 |
<p>For instance, Shell never throws away output. A program that sends unlimited |
| 711 |
output to Shell will eventually fill memory, resulting in a memory error. |
| 712 |
In contrast, some system text windows only keep the last n lines of output. |
| 713 |
A Windows console, for instance, keeps a user-settable 1 to 9999 lines, |
| 714 |
with 300 the default.</p> |
| 715 |
<p>A Tk Text widget, and hence IDLE’s Shell, displays characters (codepoints) in |
| 716 |
the BMP (Basic Multilingual Plane) subset of Unicode. Which characters are |
| 717 |
displayed with a proper glyph and which with a replacement box depends on the |
| 718 |
operating system and installed fonts. Tab characters cause the following text |
| 719 |
to begin after the next tab stop. (They occur every 8 ‘characters’). Newline |
| 720 |
characters cause following text to appear on a new line. Other control |
| 721 |
characters are ignored or displayed as a space, box, or something else, |
| 722 |
depending on the operating system and font. (Moving the text cursor through |
| 723 |
such output with arrow keys may exhibit some surprising spacing behavior.)</p> |
| 724 |
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s1">'a</span><span class="se">\t</span><span class="s1">b</span><span class="se">\a</span><span class="s1"><</span><span class="se">\x02</span><span class="s1">><</span><span class="se">\r</span><span class="s1">></span><span class="se">\b</span><span class="s1">c</span><span class="se">\n</span><span class="s1">d'</span> <span class="c1"># Enter 22 chars.</span> |
| 725 |
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> |
| 726 |
<span class="go">14</span> |
| 727 |
<span class="gp">>>> </span><span class="n">s</span> <span class="c1"># Display repr(s)</span> |
| 728 |
<span class="go">'a\tb\x07<\x02><\r>\x08c\nd'</span> |
| 729 |
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span> <span class="c1"># Display s as is.</span> |
| 730 |
<span class="go"># Result varies by OS and font. Try it.</span> |
| 731 |
</pre></div> |
| 732 |
</div> |
| 733 |
<p>The <code class="docutils literal notranslate"><span class="pre">repr</span></code> function is used for interactive echo of expression |
| 734 |
values. It returns an altered version of the input string in which |
| 735 |
control codes, some BMP codepoints, and all non-BMP codepoints are |
| 736 |
replaced with escape codes. As demonstrated above, it allows one to |
| 737 |
identify the characters in a string, regardless of how they are displayed.</p> |
| 738 |
<p>Normal and error output are generally kept separate (on separate lines) |
| 739 |
from code input and each other. They each get different highlight colors.</p> |
| 740 |
<p>For SyntaxError tracebacks, the normal ‘^’ marking where the error was |
| 741 |
detected is replaced by coloring the text with an error highlight. |
| 742 |
When code run from a file causes other exceptions, one may right click |
| 743 |
on a traceback line to jump to the corresponding line in an IDLE editor. |
| 744 |
The file will be opened if necessary.</p> |
| 745 |
<p>Shell has a special facility for squeezing output lines down to a |
| 746 |
‘Squeezed text’ label. This is done automatically |
| 747 |
for output over N lines (N = 50 by default). |
| 748 |
N can be changed in the PyShell section of the General |
| 749 |
page of the Settings dialog. Output with fewer lines can be squeezed by |
| 750 |
right clicking on the output. This can be useful lines long enough to slow |
| 751 |
down scrolling.</p> |
| 752 |
<p>Squeezed output is expanded in place by double-clicking the label. |
| 753 |
It can also be sent to the clipboard or a separate view window by |
| 754 |
right-clicking the label.</p> |
| 755 |
</section> |
| 756 |
<section id="developing-tkinter-applications"> |
| 757 |
<h3>Developing tkinter applications<a class="headerlink" href="#developing-tkinter-applications" title="Link to this heading">¶</a></h3> |
| 758 |
<p>IDLE is intentionally different from standard Python in order to |
| 759 |
facilitate development of tkinter programs. Enter <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">tkinter</span> <span class="pre">as</span> <span class="pre">tk;</span> |
| 760 |
<span class="pre">root</span> <span class="pre">=</span> <span class="pre">tk.Tk()</span></code> in standard Python and nothing appears. Enter the same |
| 761 |
in IDLE and a tk window appears. In standard Python, one must also enter |
| 762 |
<code class="docutils literal notranslate"><span class="pre">root.update()</span></code> to see the window. IDLE does the equivalent in the |
| 763 |
background, about 20 times a second, which is about every 50 milliseconds. |
| 764 |
Next enter <code class="docutils literal notranslate"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">tk.Button(root,</span> <span class="pre">text='button');</span> <span class="pre">b.pack()</span></code>. Again, |
| 765 |
nothing visibly changes in standard Python until one enters <code class="docutils literal notranslate"><span class="pre">root.update()</span></code>.</p> |
| 766 |
<p>Most tkinter programs run <code class="docutils literal notranslate"><span class="pre">root.mainloop()</span></code>, which usually does not |
| 767 |
return until the tk app is destroyed. If the program is run with |
| 768 |
<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-i</span></code> or from an IDLE editor, a <code class="docutils literal notranslate"><span class="pre">>>></span></code> shell prompt does not |
| 769 |
appear until <code class="docutils literal notranslate"><span class="pre">mainloop()</span></code> returns, at which time there is nothing left |
| 770 |
to interact with.</p> |
| 771 |
<p>When running a tkinter program from an IDLE editor, one can comment out |
| 772 |
the mainloop call. One then gets a shell prompt immediately and can |
| 773 |
interact with the live application. One just has to remember to |
| 774 |
re-enable the mainloop call when running in standard Python.</p> |
| 775 |
</section> |
| 776 |
<section id="running-without-a-subprocess"> |
| 777 |
<h3>Running without a subprocess<a class="headerlink" href="#running-without-a-subprocess" title="Link to this heading">¶</a></h3> |
| 778 |
<p>By default, IDLE executes user code in a separate subprocess via a socket, |
| 779 |
which uses the internal loopback interface. This connection is not |
| 780 |
externally visible and no data is sent to or received from the internet. |
| 781 |
If firewall software complains anyway, you can ignore it.</p> |
| 782 |
<p>If the attempt to make the socket connection fails, Idle will notify you. |
| 783 |
Such failures are sometimes transient, but if persistent, the problem |
| 784 |
may be either a firewall blocking the connection or misconfiguration of |
| 785 |
a particular system. Until the problem is fixed, one can run Idle with |
| 786 |
the -n command line switch.</p> |
| 787 |
<p>If IDLE is started with the -n command line switch it will run in a |
| 788 |
single process and will not create the subprocess which runs the RPC |
| 789 |
Python execution server. This can be useful if Python cannot create |
| 790 |
the subprocess or the RPC socket interface on your platform. However, |
| 791 |
in this mode user code is not isolated from IDLE itself. Also, the |
| 792 |
environment is not restarted when Run/Run Module (F5) is selected. If |
| 793 |
your code has been modified, you must reload() the affected modules and |
| 794 |
re-import any specific items (e.g. from foo import baz) if the changes |
| 795 |
are to take effect. For these reasons, it is preferable to run IDLE |
| 796 |
with the default subprocess if at all possible.</p> |
| 797 |
<div class="deprecated"> |
| 798 |
<p><span class="versionmodified deprecated">Deprecated since version 3.4.</span></p> |
| 799 |
</div> |
| 800 |
</section> |
| 801 |
</section> |
| 802 |
<section id="help-and-preferences"> |
| 803 |
<h2>Help and Preferences<a class="headerlink" href="#help-and-preferences" title="Link to this heading">¶</a></h2> |
| 804 |
<section id="help-sources"> |
| 805 |
<span id="id6"></span><h3>Help sources<a class="headerlink" href="#help-sources" title="Link to this heading">¶</a></h3> |
| 806 |
<p>Help menu entry “IDLE Help” displays a formatted html version of the |
| 807 |
IDLE chapter of the Library Reference. The result, in a read-only |
| 808 |
tkinter text window, is close to what one sees in a web browser. |
| 809 |
Navigate through the text with a mousewheel, |
| 810 |
the scrollbar, or up and down arrow keys held down. |
| 811 |
Or click the TOC (Table of Contents) button and select a section |
| 812 |
header in the opened box.</p> |
| 813 |
<p>Help menu entry “Python Docs” opens the extensive sources of help, |
| 814 |
including tutorials, available at <code class="docutils literal notranslate"><span class="pre">docs.python.org/x.y</span></code>, where ‘x.y’ |
| 815 |
is the currently running Python version. If your system |
| 816 |
has an off-line copy of the docs (this may be an installation option), |
| 817 |
that will be opened instead.</p> |
| 818 |
<p>Selected URLs can be added or removed from the help menu at any time using the |
| 819 |
General tab of the Configure IDLE dialog.</p> |
| 820 |
</section> |
| 821 |
<section id="setting-preferences"> |
| 822 |
<span id="preferences"></span><h3>Setting preferences<a class="headerlink" href="#setting-preferences" title="Link to this heading">¶</a></h3> |
| 823 |
<p>The font preferences, highlighting, keys, and general preferences can be |
| 824 |
changed via Configure IDLE on the Option menu. |
| 825 |
Non-default user settings are saved in a <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code> directory in the user’s |
| 826 |
home directory. Problems caused by bad user configuration files are solved |
| 827 |
by editing or deleting one or more of the files in <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code>.</p> |
| 828 |
<p>On the Font tab, see the text sample for the effect of font face and size |
| 829 |
on multiple characters in multiple languages. Edit the sample to add |
| 830 |
other characters of personal interest. Use the sample to select |
| 831 |
monospaced fonts. If particular characters have problems in Shell or an |
| 832 |
editor, add them to the top of the sample and try changing first size |
| 833 |
and then font.</p> |
| 834 |
<p>On the Highlights and Keys tab, select a built-in or custom color theme |
| 835 |
and key set. To use a newer built-in color theme or key set with older |
| 836 |
IDLEs, save it as a new custom theme or key set and it well be accessible |
| 837 |
to older IDLEs.</p> |
| 838 |
</section> |
| 839 |
<section id="idle-on-macos"> |
| 840 |
<h3>IDLE on macOS<a class="headerlink" href="#idle-on-macos" title="Link to this heading">¶</a></h3> |
| 841 |
<p>Under System Preferences: Dock, one can set “Prefer tabs when opening |
| 842 |
documents” to “Always”. This setting is not compatible with the tk/tkinter |
| 843 |
GUI framework used by IDLE, and it breaks a few IDLE features.</p> |
| 844 |
</section> |
| 845 |
<section id="extensions"> |
| 846 |
<h3>Extensions<a class="headerlink" href="#extensions" title="Link to this heading">¶</a></h3> |
| 847 |
<p>IDLE contains an extension facility. Preferences for extensions can be |
| 848 |
changed with the Extensions tab of the preferences dialog. See the |
| 849 |
beginning of config-extensions.def in the idlelib directory for further |
| 850 |
information. The only current default extension is zzdummy, an example |
| 851 |
also used for testing.</p> |
| 852 |
</section> |
| 853 |
</section> |
| 854 |
<section id="module-idlelib"> |
| 855 |
<span id="idlelib-implementation-of-idle-application"></span><h2>idlelib — implementation of IDLE application<a class="headerlink" href="#module-idlelib" title="Link to this heading">¶</a></h2> |
| 856 |
<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/idlelib">Lib/idlelib</a></p> |
| 857 |
<hr class="docutils" /> |
| 858 |
<p>The Lib/idlelib package implements the IDLE application. See the rest |
| 859 |
of this page for how to use IDLE.</p> |
| 860 |
<p>The files in idlelib are described in idlelib/README.txt. Access it |
| 861 |
either in idlelib or click Help => About IDLE on the IDLE menu. This |
| 862 |
file also maps IDLE menu items to the code that implements the item. |
| 863 |
Except for files listed under ‘Startup’, the idlelib code is ‘private’ in |
| 864 |
sense that feature changes can be backported (see <span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0434/"><strong>PEP 434</strong></a>).</p> |
| 865 |
</section> |
| 866 |
</section> |
| 867 |
|
| 868 |
|