Show More
@@ -1,1284 +1,1261 b'' | |||
|
1 | 1 | ================= |
|
2 | 2 | IPython reference |
|
3 | 3 | ================= |
|
4 | 4 | |
|
5 | 5 | .. _command_line_options: |
|
6 | 6 | |
|
7 | 7 | Command-line usage |
|
8 | 8 | ================== |
|
9 | 9 | |
|
10 | 10 | You start IPython with the command:: |
|
11 | 11 | |
|
12 | 12 | $ ipython [options] files |
|
13 | 13 | |
|
14 | .. note:: | |
|
15 | ||
|
16 | For IPython on Python 3, use ``ipython3`` in place of ``ipython``. | |
|
17 | ||
|
14 | 18 | If invoked with no options, it executes all the files listed in sequence |
|
15 | 19 | and drops you into the interpreter while still acknowledging any options |
|
16 | 20 | you may have set in your ipython_config.py. This behavior is different from |
|
17 | 21 | standard Python, which when called as python -i will only execute one |
|
18 | 22 | file and ignore your configuration setup. |
|
19 | 23 | |
|
20 | 24 | Please note that some of the configuration options are not available at |
|
21 | 25 | the command line, simply because they are not practical here. Look into |
|
22 | 26 | your configuration files for details on those. There are separate configuration |
|
23 | 27 | files for each profile, and the files look like "ipython_config.py" or |
|
24 | 28 | "ipython_config_<frontendname>.py". Profile directories look like |
|
25 | 29 | "profile_profilename" and are typically installed in the IPYTHON_DIR directory. |
|
26 | 30 | For Linux users, this will be $HOME/.config/ipython, and for other users it |
|
27 | 31 | will be $HOME/.ipython. For Windows users, $HOME resolves to C:\\Documents and |
|
28 | 32 | Settings\\YourUserName in most instances. |
|
29 | 33 | |
|
30 | 34 | |
|
31 | 35 | Eventloop integration |
|
32 | 36 | --------------------- |
|
33 | 37 | |
|
34 | 38 | Previously IPython had command line options for controlling GUI event loop |
|
35 | 39 | integration (-gthread, -qthread, -q4thread, -wthread, -pylab). As of IPython |
|
36 | 40 | version 0.11, these have been removed. Please see the new ``%gui`` |
|
37 | 41 | magic command or :ref:`this section <gui_support>` for details on the new |
|
38 | 42 | interface, or specify the gui at the commandline:: |
|
39 | 43 | |
|
40 | 44 | $ ipython --gui=qt |
|
41 | 45 | |
|
42 | 46 | |
|
43 | 47 | Regular Options |
|
44 | 48 | --------------- |
|
45 | 49 | |
|
46 | 50 | After the above threading options have been given, regular options can |
|
47 | 51 | follow in any order. All options can be abbreviated to their shortest |
|
48 |
non-ambiguous form and are case-sensitive. |
|
|
49 | used. Some options have an alternate short form, indicated after a ``|``. | |
|
52 | non-ambiguous form and are case-sensitive. | |
|
50 | 53 | |
|
51 | 54 | Most options can also be set from your configuration file. See the provided |
|
52 | 55 | example for more details on what the options do. Options given at the command |
|
53 | 56 | line override the values set in the configuration file. |
|
54 | 57 | |
|
55 | 58 | All options with a [no] prepended can be specified in negated form |
|
56 | 59 | (--no-option instead of --option) to turn the feature off. |
|
57 | 60 | |
|
58 | 61 | ``-h, --help`` print a help message and exit. |
|
59 | 62 | |
|
60 | 63 | ``--pylab, pylab=<name>`` |
|
61 | 64 | See :ref:`Matplotlib support <matplotlib_support>` |
|
62 | 65 | for more details. |
|
63 | 66 | |
|
64 | 67 | ``--autocall=<val>`` |
|
65 | 68 | Make IPython automatically call any callable object even if you |
|
66 | 69 | didn't type explicit parentheses. For example, 'str 43' becomes |
|
67 | 70 | 'str(43)' automatically. The value can be '0' to disable the feature, |
|
68 | 71 | '1' for smart autocall, where it is not applied if there are no more |
|
69 | 72 | arguments on the line, and '2' for full autocall, where all callable |
|
70 | 73 | objects are automatically called (even if no arguments are |
|
71 | 74 | present). The default is '1'. |
|
72 | 75 | |
|
73 | 76 | ``--[no-]autoindent`` |
|
74 | 77 | Turn automatic indentation on/off. |
|
75 | 78 | |
|
76 | 79 | ``--[no-]automagic`` |
|
77 | 80 | make magic commands automatic (without needing their first character |
|
78 | 81 | to be %). Type %magic at the IPython prompt for more information. |
|
79 | 82 | |
|
80 | 83 | ``--[no-]autoedit_syntax`` |
|
81 | 84 | When a syntax error occurs after editing a file, automatically |
|
82 | 85 | open the file to the trouble causing line for convenient |
|
83 | 86 | fixing. |
|
84 | 87 | |
|
85 | 88 | ``--[no-]banner`` |
|
86 | 89 | Print the initial information banner (default on). |
|
87 | 90 | |
|
88 | 91 | ``-c <command>`` |
|
89 | 92 | execute the given command string. This is similar to the -c |
|
90 | 93 | option in the normal Python interpreter. |
|
91 | 94 | |
|
92 | 95 | ``--cache-size=<n>`` |
|
93 | 96 | size of the output cache (maximum number of entries to hold in |
|
94 | 97 | memory). The default is 1000, you can change it permanently in your |
|
95 | 98 | config file. Setting it to 0 completely disables the caching system, |
|
96 | 99 | and the minimum value accepted is 20 (if you provide a value less than |
|
97 | 100 | 20, it is reset to 0 and a warning is issued) This limit is defined |
|
98 | 101 | because otherwise you'll spend more time re-flushing a too small cache |
|
99 | 102 | than working. |
|
100 | 103 | |
|
101 | 104 | ``--classic`` |
|
102 | 105 | Gives IPython a similar feel to the classic Python |
|
103 | 106 | prompt. |
|
104 | 107 | |
|
105 | 108 | ``--colors=<scheme>`` |
|
106 | 109 | Color scheme for prompts and exception reporting. Currently |
|
107 | 110 | implemented: NoColor, Linux and LightBG. |
|
108 | 111 | |
|
109 | 112 | ``--[no-]color_info`` |
|
110 | 113 | IPython can display information about objects via a set of functions, |
|
111 | 114 | and optionally can use colors for this, syntax highlighting source |
|
112 | 115 | code and various other elements. However, because this information is |
|
113 | 116 | passed through a pager (like 'less') and many pagers get confused with |
|
114 | 117 | color codes, this option is off by default. You can test it and turn |
|
115 | 118 | it on permanently in your configuration file if it works for you. As a |
|
116 | 119 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but |
|
117 | 120 | that in RedHat 7.2 doesn't. |
|
118 | 121 | |
|
119 | 122 | Test it and turn it on permanently if it works with your |
|
120 | 123 | system. The magic function %color_info allows you to toggle this |
|
121 | 124 | interactively for testing. |
|
122 | 125 | |
|
123 | 126 | ``--[no-]debug`` |
|
124 | 127 | Show information about the loading process. Very useful to pin down |
|
125 | 128 | problems with your configuration files or to get details about |
|
126 | 129 | session restores. |
|
127 | 130 | |
|
128 | 131 | ``--[no-]deep_reload`` |
|
129 | 132 | IPython can use the deep_reload module which reloads changes in |
|
130 | 133 | modules recursively (it replaces the reload() function, so you don't |
|
131 | 134 | need to change anything to use it). deep_reload() forces a full |
|
132 | 135 | reload of modules whose code may have changed, which the default |
|
133 | 136 | reload() function does not. |
|
134 | 137 | |
|
135 | 138 | When deep_reload is off, IPython will use the normal reload(), |
|
136 | 139 | but deep_reload will still be available as dreload(). This |
|
137 | 140 | feature is off by default [which means that you have both |
|
138 | 141 | normal reload() and dreload()]. |
|
139 | 142 | |
|
140 | 143 | ``--editor=<name>`` |
|
141 | 144 | Which editor to use with the %edit command. By default, |
|
142 | 145 | IPython will honor your EDITOR environment variable (if not |
|
143 | 146 | set, vi is the Unix default and notepad the Windows one). |
|
144 | 147 | Since this editor is invoked on the fly by IPython and is |
|
145 | 148 | meant for editing small code snippets, you may want to use a |
|
146 | 149 | small, lightweight editor here (in case your default EDITOR is |
|
147 | 150 | something like Emacs). |
|
148 | 151 | |
|
149 | 152 | ``--ipython_dir=<name>`` |
|
150 | 153 | name of your IPython configuration directory IPYTHON_DIR. This |
|
151 | 154 | can also be specified through the environment variable |
|
152 | 155 | IPYTHON_DIR. |
|
153 | 156 | |
|
154 | 157 | ``--logfile=<name>`` |
|
155 | 158 | specify the name of your logfile. |
|
156 | 159 | |
|
157 | 160 | This implies ``%logstart`` at the beginning of your session |
|
158 | 161 | |
|
159 | 162 | generate a log file of all input. The file is named |
|
160 | 163 | ipython_log.py in your current directory (which prevents logs |
|
161 | 164 | from multiple IPython sessions from trampling each other). You |
|
162 | 165 | can use this to later restore a session by loading your |
|
163 | 166 | logfile with ``ipython -i ipython_log.py`` |
|
164 | 167 | |
|
165 | 168 | ``--logplay=<name>`` |
|
166 | 169 | |
|
167 | 170 | NOT AVAILABLE in 0.11 |
|
168 | 171 | |
|
169 | 172 | you can replay a previous log. For restoring a session as close as |
|
170 | 173 | possible to the state you left it in, use this option (don't just run |
|
171 | 174 | the logfile). With -logplay, IPython will try to reconstruct the |
|
172 | 175 | previous working environment in full, not just execute the commands in |
|
173 | 176 | the logfile. |
|
174 | 177 | |
|
175 | 178 | When a session is restored, logging is automatically turned on |
|
176 | 179 | again with the name of the logfile it was invoked with (it is |
|
177 | 180 | read from the log header). So once you've turned logging on for |
|
178 | 181 | a session, you can quit IPython and reload it as many times as |
|
179 | 182 | you want and it will continue to log its history and restore |
|
180 | 183 | from the beginning every time. |
|
181 | 184 | |
|
182 | 185 | Caveats: there are limitations in this option. The history |
|
183 | 186 | variables _i*,_* and _dh don't get restored properly. In the |
|
184 | 187 | future we will try to implement full session saving by writing |
|
185 | 188 | and retrieving a 'snapshot' of the memory state of IPython. But |
|
186 | 189 | our first attempts failed because of inherent limitations of |
|
187 | 190 | Python's Pickle module, so this may have to wait. |
|
188 | 191 | |
|
189 | 192 | ``--[no-]messages`` |
|
190 | 193 | Print messages which IPython collects about its startup |
|
191 | 194 | process (default on). |
|
192 | 195 | |
|
193 | 196 | ``--[no-]pdb`` |
|
194 | 197 | Automatically call the pdb debugger after every uncaught |
|
195 | 198 | exception. If you are used to debugging using pdb, this puts |
|
196 | 199 | you automatically inside of it after any call (either in |
|
197 | 200 | IPython or in code called by it) which triggers an exception |
|
198 | 201 | which goes uncaught. |
|
199 | 202 | |
|
200 | 203 | ``--[no-]pprint`` |
|
201 | 204 | ipython can optionally use the pprint (pretty printer) module |
|
202 | 205 | for displaying results. pprint tends to give a nicer display |
|
203 | 206 | of nested data structures. If you like it, you can turn it on |
|
204 | 207 | permanently in your config file (default off). |
|
205 | 208 | |
|
206 | 209 | ``--profile=<name>`` |
|
207 | 210 | |
|
208 | 211 | Select the IPython profile by name. |
|
209 | 212 | |
|
210 | 213 | This is a quick way to keep and load multiple |
|
211 | 214 | config files for different tasks, especially if you use the |
|
212 | 215 | include option of config files. You can keep a basic |
|
213 | 216 | :file:`IPYTHON_DIR/profile_default/ipython_config.py` file |
|
214 | 217 | and then have other 'profiles' which |
|
215 | 218 | include this one and load extra things for particular |
|
216 | 219 | tasks. For example: |
|
217 | 220 | |
|
218 | 221 | 1. $IPYTHON_DIR/profile_default : load basic things you always want. |
|
219 | 222 | 2. $IPYTHON_DIR/profile_math : load (1) and basic math-related modules. |
|
220 | 223 | 3. $IPYTHON_DIR/profile_numeric : load (1) and Numeric and plotting modules. |
|
221 | 224 | |
|
222 | 225 | Since it is possible to create an endless loop by having |
|
223 | 226 | circular file inclusions, IPython will stop if it reaches 15 |
|
224 | 227 | recursive inclusions. |
|
225 | 228 | |
|
226 | 229 | ``InteractiveShell.prompt_in1=<string>`` |
|
227 | 230 | |
|
228 | 231 | Specify the string used for input prompts. Note that if you are using |
|
229 | 232 | numbered prompts, the number is represented with a '\#' in the |
|
230 | 233 | string. Don't forget to quote strings with spaces embedded in |
|
231 | 234 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` |
|
232 | 235 | discusses in detail all the available escapes to customize your |
|
233 | 236 | prompts. |
|
234 | 237 | |
|
235 | 238 | ``InteractiveShell.prompt_in2=<string>`` |
|
236 | 239 | Similar to the previous option, but used for the continuation |
|
237 | 240 | prompts. The special sequence '\D' is similar to '\#', but |
|
238 | 241 | with all digits replaced dots (so you can have your |
|
239 | 242 | continuation prompt aligned with your input prompt). Default: |
|
240 | 243 | ' .\D.:' (note three spaces at the start for alignment with |
|
241 | 244 | 'In [\#]'). |
|
242 | 245 | |
|
243 | 246 | ``InteractiveShell.prompt_out=<string>`` |
|
244 | 247 | String used for output prompts, also uses numbers like |
|
245 | 248 | prompt_in1. Default: 'Out[\#]:' |
|
246 | 249 | |
|
247 | 250 | ``--quick`` |
|
248 | 251 | start in bare bones mode (no config file loaded). |
|
249 | 252 | |
|
250 | 253 | ``config_file=<name>`` |
|
251 | 254 | name of your IPython resource configuration file. Normally |
|
252 | 255 | IPython loads ipython_config.py (from current directory) or |
|
253 | 256 | IPYTHON_DIR/profile_default. |
|
254 | 257 | |
|
255 | 258 | If the loading of your config file fails, IPython starts with |
|
256 | 259 | a bare bones configuration (no modules loaded at all). |
|
257 | 260 | |
|
258 | 261 | ``--[no-]readline`` |
|
259 | 262 | use the readline library, which is needed to support name |
|
260 | 263 | completion and command history, among other things. It is |
|
261 | 264 | enabled by default, but may cause problems for users of |
|
262 | 265 | X/Emacs in Python comint or shell buffers. |
|
263 | 266 | |
|
264 | 267 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support |
|
265 | 268 | IPython's readline and syntax coloring fine, only 'emacs' (M-x |
|
266 | 269 | shell and C-c !) buffers do not. |
|
267 | 270 | |
|
268 | 271 | ``--TerminalInteractiveShell.screen_length=<n>`` |
|
269 | 272 | number of lines of your screen. This is used to control |
|
270 | 273 | printing of very long strings. Strings longer than this number |
|
271 | 274 | of lines will be sent through a pager instead of directly |
|
272 | 275 | printed. |
|
273 | 276 | |
|
274 | 277 | The default value for this is 0, which means IPython will |
|
275 | 278 | auto-detect your screen size every time it needs to print certain |
|
276 | 279 | potentially long strings (this doesn't change the behavior of the |
|
277 | 280 | 'print' keyword, it's only triggered internally). If for some |
|
278 | 281 | reason this isn't working well (it needs curses support), specify |
|
279 | 282 | it yourself. Otherwise don't change the default. |
|
280 | 283 | |
|
281 | 284 | ``--TerminalInteractiveShell.separate_in=<string>`` |
|
282 | 285 | |
|
283 | 286 | separator before input prompts. |
|
284 | 287 | Default: '\n' |
|
285 | 288 | |
|
286 | 289 | ``--TerminalInteractiveShell.separate_out=<string>`` |
|
287 | 290 | separator before output prompts. |
|
288 | 291 | Default: nothing. |
|
289 | 292 | |
|
290 | 293 | ``--TerminalInteractiveShell.separate_out2=<string>`` |
|
291 | 294 | separator after output prompts. |
|
292 | 295 | Default: nothing. |
|
293 | 296 | For these three options, use the value 0 to specify no separator. |
|
294 | 297 | |
|
295 | 298 | ``--nosep`` |
|
296 | 299 | shorthand for setting the above separators to empty strings. |
|
297 | 300 | |
|
298 | 301 | Simply removes all input/output separators. |
|
299 | 302 | |
|
300 | 303 | ``--init`` |
|
301 | 304 | allows you to initialize a profile dir for configuration when you |
|
302 | 305 | install a new version of IPython or want to use a new profile. |
|
303 | 306 | Since new versions may include new command line options or example |
|
304 | 307 | files, this copies updated config files. Note that you should probably |
|
305 | 308 | use %upgrade instead,it's a safer alternative. |
|
306 | 309 | |
|
307 | 310 | ``--version`` print version information and exit. |
|
308 | 311 | |
|
309 | 312 | ``--xmode=<modename>`` |
|
310 | 313 | |
|
311 | 314 | Mode for exception reporting. |
|
312 | 315 | |
|
313 | 316 | Valid modes: Plain, Context and Verbose. |
|
314 | 317 | |
|
315 | 318 | * Plain: similar to python's normal traceback printing. |
|
316 | 319 | * Context: prints 5 lines of context source code around each |
|
317 | 320 | line in the traceback. |
|
318 | 321 | * Verbose: similar to Context, but additionally prints the |
|
319 | 322 | variables currently visible where the exception happened |
|
320 | 323 | (shortening their strings if too long). This can potentially be |
|
321 | 324 | very slow, if you happen to have a huge data structure whose |
|
322 | 325 | string representation is complex to compute. Your computer may |
|
323 | 326 | appear to freeze for a while with cpu usage at 100%. If this |
|
324 | 327 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it |
|
325 | 328 | more than once). |
|
326 | 329 | |
|
327 | 330 | Interactive use |
|
328 | 331 | =============== |
|
329 | 332 | |
|
330 | 333 | IPython is meant to work as a drop-in replacement for the standard interactive |
|
331 | 334 | interpreter. As such, any code which is valid python should execute normally |
|
332 | 335 | under IPython (cases where this is not true should be reported as bugs). It |
|
333 | 336 | does, however, offer many features which are not available at a standard python |
|
334 | 337 | prompt. What follows is a list of these. |
|
335 | 338 | |
|
336 | 339 | |
|
337 | 340 | Caution for Windows users |
|
338 | 341 | ------------------------- |
|
339 | 342 | |
|
340 | 343 | Windows, unfortunately, uses the '\\' character as a path separator. This is a |
|
341 | 344 | terrible choice, because '\\' also represents the escape character in most |
|
342 | 345 | modern programming languages, including Python. For this reason, using '/' |
|
343 | 346 | character is recommended if you have problems with ``\``. However, in Windows |
|
344 | 347 | commands '/' flags options, so you can not use it for the root directory. This |
|
345 | 348 | means that paths beginning at the root must be typed in a contrived manner |
|
346 | 349 | like: ``%copy \opt/foo/bar.txt \tmp`` |
|
347 | 350 | |
|
348 | 351 | .. _magic: |
|
349 | 352 | |
|
350 | 353 | Magic command system |
|
351 | 354 | -------------------- |
|
352 | 355 | |
|
353 | 356 | IPython will treat any line whose first character is a % as a special |
|
354 | 357 | call to a 'magic' function. These allow you to control the behavior of |
|
355 | 358 | IPython itself, plus a lot of system-type features. They are all |
|
356 | 359 | prefixed with a % character, but parameters are given without |
|
357 | 360 | parentheses or quotes. |
|
358 | 361 | |
|
359 | 362 | Example: typing ``%cd mydir`` changes your working directory to 'mydir', if it |
|
360 | 363 | exists. |
|
361 | 364 | |
|
362 | 365 | If you have 'automagic' enabled (as it by default), you don't need |
|
363 | 366 | to type in the % explicitly. IPython will scan its internal list of |
|
364 | 367 | magic functions and call one if it exists. With automagic on you can |
|
365 | 368 | then just type ``cd mydir`` to go to directory 'mydir'. The automagic |
|
366 | 369 | system has the lowest possible precedence in name searches, so defining |
|
367 | 370 | an identifier with the same name as an existing magic function will |
|
368 | 371 | shadow it for automagic use. You can still access the shadowed magic |
|
369 | 372 | function by explicitly using the % character at the beginning of the line. |
|
370 | 373 | |
|
371 | 374 | An example (with automagic on) should clarify all this: |
|
372 | 375 | |
|
373 | 376 | .. sourcecode:: ipython |
|
374 | 377 | |
|
375 | In [1]: cd ipython # %cd is called by automagic | |
|
376 | ||
|
378 | In [1]: cd ipython # %cd is called by automagic | |
|
377 | 379 | /home/fperez/ipython |
|
378 | 380 | |
|
379 | In [2]: cd=1 # now cd is just a variable | |
|
380 | ||
|
381 | In [3]: cd .. # and doesn't work as a function anymore | |
|
382 | ||
|
383 | ------------------------------ | |
|
384 | ||
|
385 | File "<console>", line 1 | |
|
386 | ||
|
387 | cd .. | |
|
388 | ||
|
389 | ^ | |
|
381 | In [2]: cd=1 # now cd is just a variable | |
|
390 | 382 | |
|
383 | In [3]: cd .. # and doesn't work as a function anymore | |
|
384 | File "<ipython-input-3-9fedb3aff56c>", line 1 | |
|
385 | cd .. | |
|
386 | ^ | |
|
391 | 387 | SyntaxError: invalid syntax |
|
392 | 388 | |
|
393 | In [4]: %cd .. # but %cd always works | |
|
394 | 389 | |
|
390 | In [4]: %cd .. # but %cd always works | |
|
395 | 391 | /home/fperez |
|
396 | 392 | |
|
397 | In [5]: del cd # if you remove the cd variable | |
|
393 | In [5]: del cd # if you remove the cd variable, automagic works again | |
|
398 | 394 | |
|
399 |
In [6]: cd ipython |
|
|
395 | In [6]: cd ipython | |
|
400 | 396 | |
|
401 | 397 | /home/fperez/ipython |
|
402 | 398 | |
|
403 | 399 | You can define your own magic functions to extend the system. The |
|
404 | 400 | following example defines a new magic command, %impall: |
|
405 | 401 | |
|
406 | 402 | .. sourcecode:: python |
|
407 | 403 | |
|
408 | 404 | ip = get_ipython() |
|
409 | 405 | |
|
410 | 406 | def doimp(self, arg): |
|
411 | ||
|
412 | 407 | ip = self.api |
|
408 | ip.ex("import %s; reload(%s); from %s import *" % (arg,arg,arg) ) | |
|
413 | 409 | |
|
414 | ip.ex("import %s; reload(%s); from %s import *" % ( | |
|
415 | ||
|
416 | arg,arg,arg) | |
|
417 | ||
|
418 | ) | |
|
410 | ip.define_magic('impall', doimp) | |
|
419 | 411 | |
|
420 | ip.expose_magic('impall', doimp) | |
|
421 | ||
|
422 | Type `%magic` for more information, including a list of all available magic | |
|
412 | Type ``%magic`` for more information, including a list of all available magic | |
|
423 | 413 | functions at any time and their docstrings. You can also type |
|
424 | %magic_function_name? (see :ref:`below <dynamic_object_info` for information on | |
|
414 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for information on | |
|
425 | 415 | the '?' system) to get information about any particular magic function you are |
|
426 | 416 | interested in. |
|
427 | 417 | |
|
428 | 418 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
429 | 419 | docstrings of all currently available magic commands. |
|
430 | 420 | |
|
431 | 421 | |
|
432 | 422 | Access to the standard Python help |
|
433 | 423 | ---------------------------------- |
|
434 | 424 | |
|
435 | As of Python 2.1, a help system is available with access to object docstrings | |
|
436 | and the Python manuals. Simply type 'help' (no quotes) to access it. You can | |
|
437 | also type help(object) to obtain information about a given object, and | |
|
438 | help('keyword') for information on a keyword. As noted :ref:`here | |
|
439 | <accessing_help>`, you need to properly configure your environment variable | |
|
440 | PYTHONDOCS for this feature to work correctly. | |
|
425 | Simply type ``help()`` to access Python's standard help system. You can | |
|
426 | also type ``help(object)`` for information about a given object, or | |
|
427 | ``help('keyword')`` for information on a keyword. You may need to configure your | |
|
428 | PYTHONDOCS environment variable for this feature to work correctly. | |
|
441 | 429 | |
|
442 | 430 | .. _dynamic_object_info: |
|
443 | 431 | |
|
444 | 432 | Dynamic object information |
|
445 | 433 | -------------------------- |
|
446 | 434 | |
|
447 | 435 | Typing ``?word`` or ``word?`` prints detailed information about an object. If |
|
448 |
certain strings in the object are too long ( |
|
|
436 | certain strings in the object are too long (e.g. function signatures) they get | |
|
449 | 437 | snipped in the center for brevity. This system gives access variable types and |
|
450 | values, full source code for any object (if available), function prototypes and | |
|
451 | other useful information. | |
|
438 | values, docstrings, function prototypes and other useful information. | |
|
439 | ||
|
440 | If the information will not fit in the terminal, it is displayed in a pager | |
|
441 | (``less`` if available, otherwise a basic internal pager). | |
|
452 | 442 | |
|
453 |
Typing ``??word`` or ``word??`` gives access to the full information |
|
|
454 | snipping long strings. Long strings are sent to the screen through the | |
|
455 | less pager if longer than the screen and printed otherwise. On systems | |
|
456 | lacking the less command, IPython uses a very basic internal pager. | |
|
443 | Typing ``??word`` or ``word??`` gives access to the full information, including | |
|
444 | the source code where possible. Long strings are not snipped. | |
|
457 | 445 | |
|
458 | 446 | The following magic functions are particularly useful for gathering |
|
459 | 447 | information about your working environment. You can get more details by |
|
460 |
typing ``%magic`` or querying them individually ( |
|
|
461 |
|
|
|
448 | typing ``%magic`` or querying them individually (``%function_name?``); | |
|
449 | this is just a summary: | |
|
462 | 450 | |
|
463 | 451 | * **%pdoc <object>**: Print (or run through a pager if too long) the |
|
464 | 452 | docstring for an object. If the given object is a class, it will |
|
465 | 453 | print both the class and the constructor docstrings. |
|
466 | 454 | * **%pdef <object>**: Print the definition header for any callable |
|
467 | 455 | object. If the object is a class, print the constructor information. |
|
468 | 456 | * **%psource <object>**: Print (or run through a pager if too long) |
|
469 | 457 | the source code for an object. |
|
470 | 458 | * **%pfile <object>**: Show the entire source file where an object was |
|
471 | 459 | defined via a pager, opening it at the line where the object |
|
472 | 460 | definition begins. |
|
473 | 461 | * **%who/%whos**: These functions give information about identifiers |
|
474 | 462 | you have defined interactively (not things you loaded or defined |
|
475 | 463 | in your configuration files). %who just prints a list of |
|
476 | 464 | identifiers and %whos prints a table with some basic details about |
|
477 | 465 | each identifier. |
|
478 | 466 | |
|
479 | 467 | Note that the dynamic object information functions (?/??, ``%pdoc``, |
|
480 | ``%pfile``, ``%pdef``, ``%psource``) give you access to documentation even on | |
|
481 | things which are not really defined as separate identifiers. Try for example | |
|
482 | typing {}.get? or after doing import os, type ``os.path.abspath??``. | |
|
468 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as | |
|
469 | directly on variables. For example, after doing ``import os``, you can use | |
|
470 | ``os.path.abspath??``. | |
|
483 | 471 | |
|
484 | 472 | .. _readline: |
|
485 | 473 | |
|
486 | 474 | Readline-based features |
|
487 | 475 | ----------------------- |
|
488 | 476 | |
|
489 | 477 | These features require the GNU readline library, so they won't work if your |
|
490 | 478 | Python installation lacks readline support. We will first describe the default |
|
491 | 479 | behavior IPython uses, and then how to change it to suit your preferences. |
|
492 | 480 | |
|
493 | 481 | |
|
494 | 482 | Command line completion |
|
495 | 483 | +++++++++++++++++++++++ |
|
496 | 484 | |
|
497 | 485 | At any time, hitting TAB will complete any available python commands or |
|
498 | 486 | variable names, and show you a list of the possible completions if |
|
499 | 487 | there's no unambiguous one. It will also complete filenames in the |
|
500 | 488 | current directory if no python names match what you've typed so far. |
|
501 | 489 | |
|
502 | 490 | |
|
503 | 491 | Search command history |
|
504 | 492 | ++++++++++++++++++++++ |
|
505 | 493 | |
|
506 | 494 | IPython provides two ways for searching through previous input and thus |
|
507 | 495 | reduce the need for repetitive typing: |
|
508 | 496 | |
|
509 | 497 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n |
|
510 | 498 | (next,down) to search through only the history items that match |
|
511 | 499 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank |
|
512 | 500 | prompt, they just behave like normal arrow keys. |
|
513 | 501 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system |
|
514 | 502 | searches your history for lines that contain what you've typed so |
|
515 | 503 | far, completing as much as it can. |
|
516 | 504 | |
|
517 | 505 | |
|
518 | 506 | Persistent command history across sessions |
|
519 | 507 | ++++++++++++++++++++++++++++++++++++++++++ |
|
520 | 508 | |
|
521 | 509 | IPython will save your input history when it leaves and reload it next |
|
522 | 510 | time you restart it. By default, the history file is named |
|
523 | 511 | $IPYTHON_DIR/profile_<name>/history.sqlite. This allows you to keep |
|
524 | 512 | separate histories related to various tasks: commands related to |
|
525 | 513 | numerical work will not be clobbered by a system shell history, for |
|
526 | 514 | example. |
|
527 | 515 | |
|
528 | 516 | |
|
529 | 517 | Autoindent |
|
530 | 518 | ++++++++++ |
|
531 | 519 | |
|
532 | 520 | IPython can recognize lines ending in ':' and indent the next line, |
|
533 | 521 | while also un-indenting automatically after 'raise' or 'return'. |
|
534 | 522 | |
|
535 | 523 | This feature uses the readline library, so it will honor your |
|
536 | 524 | :file:`~/.inputrc` configuration (or whatever file your INPUTRC variable points |
|
537 | 525 | to). Adding the following lines to your :file:`.inputrc` file can make |
|
538 | 526 | indenting/unindenting more convenient (M-i indents, M-u unindents):: |
|
539 | 527 | |
|
540 | 528 | $if Python |
|
541 | 529 | "\M-i": " " |
|
542 | 530 | "\M-u": "\d\d\d\d" |
|
543 | 531 | $endif |
|
544 | 532 | |
|
545 | 533 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
546 | 534 | |
|
547 | 535 | .. warning:: |
|
548 | 536 | |
|
549 | 537 | Setting the above indents will cause problems with unicode text entry in |
|
550 | 538 | the terminal. |
|
551 | 539 | |
|
552 | 540 | .. warning:: |
|
553 | 541 | |
|
554 | 542 | Autoindent is ON by default, but it can cause problems with the pasting of |
|
555 | 543 | multi-line indented code (the pasted code gets re-indented on each line). A |
|
556 | 544 | magic function %autoindent allows you to toggle it on/off at runtime. You |
|
557 | 545 | can also disable it permanently on in your :file:`ipython_config.py` file |
|
558 | 546 | (set TerminalInteractiveShell.autoindent=False). |
|
559 | 547 | |
|
560 |
If you want to paste multiple lines, it is recommended that |
|
|
561 | ``%paste``. | |
|
548 | If you want to paste multiple lines in the terminal, it is recommended that | |
|
549 | you use ``%paste``. | |
|
562 | 550 | |
|
563 | 551 | |
|
564 | 552 | Customizing readline behavior |
|
565 | 553 | +++++++++++++++++++++++++++++ |
|
566 | 554 | |
|
567 | 555 | All these features are based on the GNU readline library, which has an |
|
568 | 556 | extremely customizable interface. Normally, readline is configured via a |
|
569 | 557 | file which defines the behavior of the library; the details of the |
|
570 | 558 | syntax for this can be found in the readline documentation available |
|
571 | 559 | with your system or on the Internet. IPython doesn't read this file (if |
|
572 | 560 | it exists) directly, but it does support passing to readline valid |
|
573 | 561 | options via a simple interface. In brief, you can customize readline by |
|
574 | 562 | setting the following options in your configuration file (note |
|
575 | 563 | that these options can not be specified at the command line): |
|
576 | 564 | |
|
577 | * **readline_parse_and_bind**: this option can appear as many times as | |
|
578 | you want, each time defining a string to be executed via a | |
|
579 | readline.parse_and_bind() command. The syntax for valid commands | |
|
565 | * **readline_parse_and_bind**: this holds a list of strings to be executed | |
|
566 | via a readline.parse_and_bind() command. The syntax for valid commands | |
|
580 | 567 | of this kind can be found by reading the documentation for the GNU |
|
581 | 568 | readline library, as these commands are of the kind which readline |
|
582 | 569 | accepts in its configuration file. |
|
583 | 570 | * **readline_remove_delims**: a string of characters to be removed |
|
584 | 571 | from the default word-delimiters list used by readline, so that |
|
585 | 572 | completions may be performed on strings which contain them. Do not |
|
586 | 573 | change the default value unless you know what you're doing. |
|
587 | * **readline_omit__names**: when tab-completion is enabled, hitting | |
|
588 | <tab> after a '.' in a name will complete all attributes of an | |
|
589 | object, including all the special methods whose names include | |
|
590 | double underscores (like __getitem__ or __class__). If you'd | |
|
591 | rather not see these names by default, you can set this option to | |
|
592 | 1. Note that even when this option is set, you can still see those | |
|
593 | names by explicitly typing a _ after the period and hitting <tab>: | |
|
594 | 'name._<tab>' will always complete attribute names starting with '_'. | |
|
595 | ||
|
596 | This option is off by default so that new users see all | |
|
597 | attributes of any objects they are dealing with. | |
|
598 | 574 | |
|
599 | 575 | You will find the default values in your configuration file. |
|
600 | 576 | |
|
601 | 577 | |
|
602 | 578 | Session logging and restoring |
|
603 | 579 | ----------------------------- |
|
604 | 580 | |
|
605 | 581 | You can log all input from a session either by starting IPython with the |
|
606 | 582 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) |
|
607 | 583 | or by activating the logging at any moment with the magic function %logstart. |
|
608 | 584 | |
|
609 | 585 | Log files can later be reloaded by running them as scripts and IPython |
|
610 | 586 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
611 | 587 | restoring the state of a previous session. This feature is not quite |
|
612 | 588 | perfect, but can still be useful in many cases. |
|
613 | 589 | |
|
614 | 590 | The log files can also be used as a way to have a permanent record of |
|
615 | 591 | any code you wrote while experimenting. Log files are regular text files |
|
616 | 592 | which you can later open in your favorite text editor to extract code or |
|
617 | 593 | to 'clean them up' before using them to replay a session. |
|
618 | 594 | |
|
619 | 595 | The `%logstart` function for activating logging in mid-session is used as |
|
620 | 596 | follows:: |
|
621 | 597 | |
|
622 | 598 | %logstart [log_name [log_mode]] |
|
623 | 599 | |
|
624 | 600 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
625 | 601 | current working directory, in 'rotate' mode (see below). |
|
626 | 602 | |
|
627 | 603 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
628 | 604 | history up to that point and then continues logging. |
|
629 | 605 | |
|
630 | 606 | %logstart takes a second optional parameter: logging mode. This can be |
|
631 | 607 | one of (note that the modes are given unquoted): |
|
632 | 608 | |
|
633 | 609 | * [over:] overwrite existing log_name. |
|
634 | 610 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
635 | 611 | * [append:] well, that says it. |
|
636 | 612 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
637 | 613 | |
|
638 | 614 | The %logoff and %logon functions allow you to temporarily stop and |
|
639 | 615 | resume logging to a file which had previously been started with |
|
640 | 616 | %logstart. They will fail (with an explanation) if you try to use them |
|
641 | 617 | before logging has been started. |
|
642 | 618 | |
|
643 | 619 | .. _system_shell_access: |
|
644 | 620 | |
|
645 | 621 | System shell access |
|
646 | 622 | ------------------- |
|
647 | 623 | |
|
648 | 624 | Any input line beginning with a ! character is passed verbatim (minus |
|
649 | 625 | the !, of course) to the underlying operating system. For example, |
|
650 | 626 | typing ``!ls`` will run 'ls' in the current directory. |
|
651 | 627 | |
|
652 | 628 | Manual capture of command output |
|
653 | 629 | -------------------------------- |
|
654 | 630 | |
|
655 | 631 | You can assign the result of a system command to a Python variable with the |
|
656 | 632 | syntax ``myfiles = !ls``. This gets machine readable output from stdout |
|
657 | 633 | (e.g. without colours), and splits on newlines. To explicitly get this sort of |
|
658 | 634 | output without assigning to a variable, use two exclamation marks (``!!ls``) or |
|
659 | 635 | the ``%sx`` magic command. |
|
660 | 636 | |
|
661 | 637 | The captured list has some convenience features. ``myfiles.n`` or ``myfiles.s`` |
|
662 | 638 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` |
|
663 | 639 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. |
|
664 | 640 | |
|
665 | 641 | IPython also allows you to expand the value of python variables when |
|
666 | 642 | making system calls. Wrap variables or expressions in {braces}:: |
|
667 | 643 | |
|
668 | 644 | In [1]: pyvar = 'Hello world' |
|
669 | 645 | In [2]: !echo "A python variable: {pyvar}" |
|
670 | 646 | A python variable: Hello world |
|
671 | 647 | In [3]: import math |
|
672 | 648 | In [4]: x = 8 |
|
673 | 649 | In [5]: !echo {math.factorial(x)} |
|
674 | 650 | 40320 |
|
675 | 651 | |
|
676 | 652 | For simple cases, you can alternatively prepend $ to a variable name:: |
|
677 | 653 | |
|
678 | 654 | In [6]: !echo $sys.argv |
|
679 | 655 | [/home/fperez/usr/bin/ipython] |
|
680 | 656 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ |
|
681 | 657 | A system variable: /home/fperez |
|
682 | 658 | |
|
683 | 659 | System command aliases |
|
684 | 660 | ---------------------- |
|
685 | 661 | |
|
686 | 662 | The %alias magic function allows you to define magic functions which are in fact |
|
687 | 663 | system shell commands. These aliases can have parameters. |
|
688 | 664 | |
|
689 | 665 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' |
|
690 | 666 | |
|
691 |
Then, typing `` |
|
|
667 | Then, typing ``alias_name params`` will execute the system command 'cmd | |
|
692 | 668 | params' (from your underlying operating system). |
|
693 | 669 | |
|
694 | 670 | You can also define aliases with parameters using %s specifiers (one per |
|
695 |
parameter). The following example defines the |
|
|
671 | parameter). The following example defines the parts function as an | |
|
696 | 672 | alias to the command 'echo first %s second %s' where each %s will be |
|
697 | 673 | replaced by a positional parameter to the call to %parts:: |
|
698 | 674 | |
|
699 |
In [1]: alias parts echo first %s second %s |
|
|
700 |
In [2]: |
|
|
701 |
first A second B |
|
|
702 |
In [3]: |
|
|
703 | Incorrect number of arguments: 2 expected. | |
|
704 | parts is an alias to: 'echo first %s second %s' | |
|
675 | In [1]: %alias parts echo first %s second %s | |
|
676 | In [2]: parts A B | |
|
677 | first A second B | |
|
678 | In [3]: parts A | |
|
679 | ERROR: Alias <parts> requires 2 arguments, 1 given. | |
|
705 | 680 | |
|
706 | 681 | If called with no parameters, %alias prints the table of currently |
|
707 | 682 | defined aliases. |
|
708 | 683 | |
|
709 | 684 | The %rehashx magic allows you to load your entire $PATH as |
|
710 | 685 | ipython aliases. See its docstring for further details. |
|
711 | 686 | |
|
712 | 687 | |
|
713 | 688 | .. _dreload: |
|
714 | 689 | |
|
715 | 690 | Recursive reload |
|
716 | 691 | ---------------- |
|
717 | 692 | |
|
718 | The dreload function does a recursive reload of a module: changes made | |
|
719 | to the module since you imported will actually be available without | |
|
720 | having to exit. | |
|
693 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a | |
|
694 | module: changes made to any of its dependencies will be reloaded without | |
|
695 | having to exit. To start using it, do:: | |
|
696 | ||
|
697 | from IPython.lib.deepreload import reload as dreload | |
|
721 | 698 | |
|
722 | 699 | |
|
723 | 700 | Verbose and colored exception traceback printouts |
|
724 | 701 | ------------------------------------------------- |
|
725 | 702 | |
|
726 | 703 | IPython provides the option to see very detailed exception tracebacks, |
|
727 | 704 | which can be especially useful when debugging large programs. You can |
|
728 | 705 | run any Python file with the %run function to benefit from these |
|
729 | 706 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
730 | 707 | be colored (if your terminal supports it) which makes them much easier |
|
731 | 708 | to parse visually. |
|
732 | 709 | |
|
733 | 710 | See the magic xmode and colors functions for details (just type %magic). |
|
734 | 711 | |
|
735 | 712 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
736 | 713 | module, now part of the standard Python library. |
|
737 | 714 | |
|
738 | 715 | |
|
739 | 716 | .. _input_caching: |
|
740 | 717 | |
|
741 | 718 | Input caching system |
|
742 | 719 | -------------------- |
|
743 | 720 | |
|
744 | 721 | IPython offers numbered prompts (In/Out) with input and output caching |
|
745 | 722 | (also referred to as 'input history'). All input is saved and can be |
|
746 | 723 | retrieved as variables (besides the usual arrow key recall), in |
|
747 | 724 | addition to the %rep magic command that brings a history entry |
|
748 | 725 | up for editing on the next command line. |
|
749 | 726 | |
|
750 | 727 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
751 | 728 | |
|
752 | 729 | * _i, _ii, _iii: store previous, next previous and next-next previous inputs. |
|
753 | 730 | * In, _ih : a list of all inputs; _ih[n] is the input from line n. If you |
|
754 | 731 | overwrite In with a variable of your own, you can remake the assignment to the |
|
755 | 732 | internal list with a simple ``In=_ih``. |
|
756 | 733 | |
|
757 | 734 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
758 | 735 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. |
|
759 | 736 | |
|
760 | 737 | For example, what you typed at prompt 14 is available as _i14, _ih[14] |
|
761 | 738 | and In[14]. |
|
762 | 739 | |
|
763 | 740 | This allows you to easily cut and paste multi line interactive prompts |
|
764 | 741 | by printing them out: they print like a clean string, without prompt |
|
765 | 742 | characters. You can also manipulate them like regular variables (they |
|
766 | 743 | are strings), modify or exec them (typing ``exec _i9`` will re-execute the |
|
767 | 744 | contents of input prompt 9. |
|
768 | 745 | |
|
769 | 746 | You can also re-execute multiple lines of input easily by using the |
|
770 | magic %macro function (which automates the process and allows | |
|
771 | re-execution without having to type 'exec' every time). The macro system | |
|
772 | also allows you to re-execute previous lines which include magic | |
|
773 | function calls (which require special processing). Type %macro? for more details | |
|
774 | on the macro system. | |
|
747 | magic %rerun or %macro functions. The macro system also allows you to re-execute | |
|
748 | previous lines which include magic function calls (which require special | |
|
749 | processing). Type %macro? for more details on the macro system. | |
|
775 | 750 | |
|
776 | 751 | A history function %hist allows you to see any part of your input |
|
777 | 752 | history by printing a range of the _i variables. |
|
778 | 753 | |
|
779 | 754 | You can also search ('grep') through your history by typing |
|
780 | 755 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, |
|
781 | 756 | etc. You can bring history entries listed by '%hist -g' up for editing |
|
782 | 757 | with the %recall command, or run them immediately with %rerun. |
|
783 | 758 | |
|
784 | 759 | .. _output_caching: |
|
785 | 760 | |
|
786 | 761 | Output caching system |
|
787 | 762 | --------------------- |
|
788 | 763 | |
|
789 | 764 | For output that is returned from actions, a system similar to the input |
|
790 | 765 | cache exists but using _ instead of _i. Only actions that produce a |
|
791 | 766 | result (NOT assignments, for example) are cached. If you are familiar |
|
792 | 767 | with Mathematica, IPython's _ variables behave exactly like |
|
793 | 768 | Mathematica's % variables. |
|
794 | 769 | |
|
795 | 770 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
796 | 771 | |
|
797 | 772 | * [_] (a single underscore) : stores previous output, like Python's |
|
798 | 773 | default interpreter. |
|
799 | 774 | * [__] (two underscores): next previous. |
|
800 | 775 | * [___] (three underscores): next-next previous. |
|
801 | 776 | |
|
802 | 777 | Additionally, global variables named _<n> are dynamically created (<n> |
|
803 | 778 | being the prompt counter), such that the result of output <n> is always |
|
804 | 779 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
805 | 780 | _21). |
|
806 | 781 | |
|
807 |
These |
|
|
782 | These variables are also stored in a global dictionary (not a | |
|
808 | 783 | list, since it only has entries for lines which returned a result) |
|
809 | 784 | available under the names _oh and Out (similar to _ih and In). So the |
|
810 | 785 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you |
|
811 | 786 | accidentally overwrite the Out variable you can recover it by typing |
|
812 | 787 | 'Out=_oh' at the prompt. |
|
813 | 788 | |
|
814 | 789 | This system obviously can potentially put heavy memory demands on your |
|
815 | 790 | system, since it prevents Python's garbage collector from removing any |
|
816 | 791 | previously computed results. You can control how many results are kept |
|
817 | 792 | in memory with the option (at the command line or in your configuration |
|
818 | 793 | file) cache_size. If you set it to 0, the whole system is completely |
|
819 | 794 | disabled and the prompts revert to the classic '>>>' of normal Python. |
|
820 | 795 | |
|
821 | 796 | |
|
822 | 797 | Directory history |
|
823 | 798 | ----------------- |
|
824 | 799 | |
|
825 | 800 | Your history of visited directories is kept in the global list _dh, and |
|
826 | 801 | the magic %cd command can be used to go to any entry in that list. The |
|
827 | 802 | %dhist command allows you to view this history. Do ``cd -<TAB>`` to |
|
828 | 803 | conveniently view the directory history. |
|
829 | 804 | |
|
830 | 805 | |
|
831 | 806 | Automatic parentheses and quotes |
|
832 | 807 | -------------------------------- |
|
833 | 808 | |
|
834 | 809 | These features were adapted from Nathan Gray's LazyPython. They are |
|
835 | 810 | meant to allow less typing for common situations. |
|
836 | 811 | |
|
837 | 812 | |
|
838 | 813 | Automatic parentheses |
|
839 | --------------------- | |
|
814 | +++++++++++++++++++++ | |
|
840 | 815 | |
|
841 | 816 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
842 | 817 | (notice the commas between the arguments):: |
|
843 | 818 | |
|
844 |
|
|
|
845 | ||
|
846 | and the input will be translated to this:: | |
|
847 | ||
|
848 | -> callable_ob(arg1, arg2, arg3) | |
|
819 | In [1]: callable_ob arg1, arg2, arg3 | |
|
820 | ------> callable_ob(arg1, arg2, arg3) | |
|
849 | 821 | |
|
850 | 822 | You can force automatic parentheses by using '/' as the first character |
|
851 | 823 | of a line. For example:: |
|
852 | 824 | |
|
853 |
|
|
|
825 | In [2]: /globals # becomes 'globals()' | |
|
854 | 826 | |
|
855 | 827 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
856 | 828 | |
|
857 |
|
|
|
829 | In [3]: print /globals # syntax error | |
|
858 | 830 | |
|
859 | 831 | In most cases the automatic algorithm should work, so you should rarely |
|
860 | 832 | need to explicitly invoke /. One notable exception is if you are trying |
|
861 | 833 | to call a function with a list of tuples as arguments (the parenthesis |
|
862 | 834 | will confuse IPython):: |
|
863 | 835 | |
|
864 |
In [ |
|
|
836 | In [4]: zip (1,2,3),(4,5,6) # won't work | |
|
865 | 837 | |
|
866 | 838 | but this will work:: |
|
867 | 839 | |
|
868 |
In [ |
|
|
869 | ---> zip ((1,2,3),(4,5,6)) | |
|
870 |
Out[ |
|
|
840 | In [5]: /zip (1,2,3),(4,5,6) | |
|
841 | ------> zip ((1,2,3),(4,5,6)) | |
|
842 | Out[5]: [(1, 4), (2, 5), (3, 6)] | |
|
871 | 843 | |
|
872 | 844 | IPython tells you that it has altered your command line by displaying |
|
873 | 845 | the new command line preceded by ->. e.g.:: |
|
874 | 846 | |
|
875 |
In [ |
|
|
876 |
----> callable |
|
|
847 | In [6]: callable list | |
|
848 | ------> callable(list) | |
|
877 | 849 | |
|
878 | 850 | |
|
879 | 851 | Automatic quoting |
|
880 | ----------------- | |
|
852 | +++++++++++++++++ | |
|
881 | 853 | |
|
882 | 854 | You can force automatic quoting of a function's arguments by using ',' |
|
883 | 855 | or ';' as the first character of a line. For example:: |
|
884 | 856 | |
|
885 |
|
|
|
857 | In [1]: ,my_function /home/me # becomes my_function("/home/me") | |
|
886 | 858 | |
|
887 |
If you use ';' |
|
|
888 |
|
|
|
859 | If you use ';' the whole argument is quoted as a single string, while ',' splits | |
|
860 | on whitespace:: | |
|
889 | 861 | |
|
890 |
|
|
|
862 | In [2]: ,my_function a b c # becomes my_function("a","b","c") | |
|
891 | 863 | |
|
892 |
|
|
|
864 | In [3]: ;my_function a b c # becomes my_function("a b c") | |
|
893 | 865 | |
|
894 | 866 | Note that the ',' or ';' MUST be the first character on the line! This |
|
895 | 867 | won't work:: |
|
896 | 868 | |
|
897 |
|
|
|
869 | In [4]: x = ,my_function /home/me # syntax error | |
|
898 | 870 | |
|
899 | 871 | IPython as your default Python environment |
|
900 | 872 | ========================================== |
|
901 | 873 | |
|
902 | 874 | Python honors the environment variable PYTHONSTARTUP and will execute at |
|
903 |
startup the file referenced by this variable. If you put |
|
|
904 | this file the following two lines of code:: | |
|
875 | startup the file referenced by this variable. If you put the following code at | |
|
876 | the end of that file, then IPython will be your working environment anytime you | |
|
877 | start Python:: | |
|
905 | 878 | |
|
906 | 879 | from IPython.frontend.terminal.ipapp import launch_new_instance |
|
907 | 880 | launch_new_instance() |
|
908 | 881 | raise SystemExit |
|
909 | 882 | |
|
910 | then IPython will be your working environment anytime you start Python. | |
|
911 | 883 | The ``raise SystemExit`` is needed to exit Python when |
|
912 | 884 | it finishes, otherwise you'll be back at the normal Python '>>>' |
|
913 | 885 | prompt. |
|
914 | 886 | |
|
915 | 887 | This is probably useful to developers who manage multiple Python |
|
916 | 888 | versions and don't want to have correspondingly multiple IPython |
|
917 | 889 | versions. Note that in this mode, there is no way to pass IPython any |
|
918 | 890 | command-line options, as those are trapped first by Python itself. |
|
919 | 891 | |
|
920 | 892 | .. _Embedding: |
|
921 | 893 | |
|
922 | 894 | Embedding IPython |
|
923 | 895 | ================= |
|
924 | 896 | |
|
925 | 897 | It is possible to start an IPython instance inside your own Python |
|
926 | 898 | programs. This allows you to evaluate dynamically the state of your |
|
927 | 899 | code, operate with your variables, analyze them, etc. Note however that |
|
928 | 900 | any changes you make to values while in the shell do not propagate back |
|
929 | 901 | to the running code, so it is safe to modify your values because you |
|
930 | 902 | won't break your code in bizarre ways by doing so. |
|
931 | 903 | |
|
904 | .. note:: | |
|
905 | ||
|
906 | At present, trying to embed IPython from inside IPython causes problems. Run | |
|
907 | the code samples below outside IPython. | |
|
908 | ||
|
932 | 909 | This feature allows you to easily have a fully functional python |
|
933 | 910 | environment for doing object introspection anywhere in your code with a |
|
934 | 911 | simple function call. In some cases a simple print statement is enough, |
|
935 | 912 | but if you need to do more detailed analysis of a code fragment this |
|
936 | 913 | feature can be very valuable. |
|
937 | 914 | |
|
938 | 915 | It can also be useful in scientific computing situations where it is |
|
939 | 916 | common to need to do some automatic, computationally intensive part and |
|
940 | 917 | then stop to look at data, plots, etc. |
|
941 | 918 | Opening an IPython instance will give you full access to your data and |
|
942 | 919 | functions, and you can resume program execution once you are done with |
|
943 | 920 | the interactive part (perhaps to stop again later, as many times as |
|
944 | 921 | needed). |
|
945 | 922 | |
|
946 | 923 | The following code snippet is the bare minimum you need to include in |
|
947 | 924 | your Python programs for this to work (detailed examples follow later):: |
|
948 | 925 | |
|
949 | 926 | from IPython import embed |
|
950 | 927 | |
|
951 | 928 | embed() # this call anywhere in your program will start IPython |
|
952 | 929 | |
|
953 | 930 | You can run embedded instances even in code which is itself being run at |
|
954 | 931 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
955 | 932 | to get lost as to where you are (in your top-level IPython or in your |
|
956 | 933 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
957 | 934 | to something different for the embedded instances. The code examples |
|
958 | 935 | below illustrate this. |
|
959 | 936 | |
|
960 | 937 | You can also have multiple IPython instances in your program and open |
|
961 | 938 | them separately, for example with different options for data |
|
962 | 939 | presentation. If you close and open the same instance multiple times, |
|
963 | 940 | its prompt counters simply continue from each execution to the next. |
|
964 | 941 | |
|
965 | 942 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` |
|
966 | 943 | module for more details on the use of this system. |
|
967 | 944 | |
|
968 | 945 | The following sample file illustrating how to use the embedding |
|
969 | 946 | functionality is provided in the examples directory as example-embed.py. |
|
970 | 947 | It should be fairly self-explanatory: |
|
971 | 948 | |
|
972 | 949 | .. literalinclude:: ../../examples/core/example-embed.py |
|
973 | 950 | :language: python |
|
974 | 951 | |
|
975 | 952 | Once you understand how the system functions, you can use the following |
|
976 | 953 | code fragments in your programs which are ready for cut and paste: |
|
977 | 954 | |
|
978 | 955 | .. literalinclude:: ../../examples/core/example-embed-short.py |
|
979 | 956 | :language: python |
|
980 | 957 | |
|
981 | 958 | Using the Python debugger (pdb) |
|
982 | 959 | =============================== |
|
983 | 960 | |
|
984 | 961 | Running entire programs via pdb |
|
985 | 962 | ------------------------------- |
|
986 | 963 | |
|
987 | 964 | pdb, the Python debugger, is a powerful interactive debugger which |
|
988 | 965 | allows you to step through code, set breakpoints, watch variables, |
|
989 | 966 | etc. IPython makes it very easy to start any script under the control |
|
990 | 967 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
991 | 968 | function or not. For this, simply type '%run -d myscript' at an |
|
992 | 969 | IPython prompt. See the %run command's documentation (via '%run?' or |
|
993 | 970 | in Sec. magic_ for more details, including how to control where pdb |
|
994 | 971 | will stop execution first. |
|
995 | 972 | |
|
996 | 973 | For more information on the use of the pdb debugger, read the included |
|
997 | 974 | pdb.doc file (part of the standard Python distribution). On a stock |
|
998 | 975 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the |
|
999 | 976 | easiest way to read it is by using the help() function of the pdb module |
|
1000 | 977 | as follows (in an IPython prompt):: |
|
1001 | 978 | |
|
1002 | 979 | In [1]: import pdb |
|
1003 | 980 | In [2]: pdb.help() |
|
1004 | 981 | |
|
1005 | 982 | This will load the pdb.doc document in a file viewer for you automatically. |
|
1006 | 983 | |
|
1007 | 984 | |
|
1008 | 985 | Automatic invocation of pdb on exceptions |
|
1009 | 986 | ----------------------------------------- |
|
1010 | 987 | |
|
1011 | IPython, if started with the -pdb option (or if the option is set in | |
|
1012 |
your |
|
|
988 | IPython, if started with the ``--pdb`` option (or if the option is set in | |
|
989 | your config file) can call the Python pdb debugger every time your code | |
|
1013 | 990 | triggers an uncaught exception. This feature |
|
1014 | 991 | can also be toggled at any time with the %pdb magic command. This can be |
|
1015 | 992 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
1016 | 993 | opens up at the point in your code which triggered the exception, and |
|
1017 | 994 | while your program is at this point 'dead', all the data is still |
|
1018 | 995 | available and you can walk up and down the stack frame and understand |
|
1019 | 996 | the origin of the problem. |
|
1020 | 997 | |
|
1021 | 998 | Furthermore, you can use these debugging facilities both with the |
|
1022 | 999 | embedded IPython mode and without IPython at all. For an embedded shell |
|
1023 | 1000 | (see sec. Embedding_), simply call the constructor with |
|
1024 |
|
|
|
1001 | ``--pdb`` in the argument string and pdb will automatically be called if an | |
|
1025 | 1002 | uncaught exception is triggered by your code. |
|
1026 | 1003 | |
|
1027 | 1004 | For stand-alone use of the feature in your programs which do not use |
|
1028 | 1005 | IPython at all, put the following lines toward the top of your 'main' |
|
1029 | 1006 | routine:: |
|
1030 | 1007 | |
|
1031 | 1008 | import sys |
|
1032 | 1009 | from IPython.core import ultratb |
|
1033 | 1010 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
1034 | 1011 | color_scheme='Linux', call_pdb=1) |
|
1035 | 1012 | |
|
1036 | 1013 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
1037 | 1014 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
1038 | 1015 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
1039 | options which can be set in IPython with -colors and -xmode. | |
|
1016 | options which can be set in IPython with ``--colors`` and ``--xmode``. | |
|
1040 | 1017 | |
|
1041 | 1018 | This will give any of your programs detailed, colored tracebacks with |
|
1042 | 1019 | automatic invocation of pdb. |
|
1043 | 1020 | |
|
1044 | 1021 | |
|
1045 | 1022 | Extensions for syntax processing |
|
1046 | 1023 | ================================ |
|
1047 | 1024 | |
|
1048 | 1025 | This isn't for the faint of heart, because the potential for breaking |
|
1049 | 1026 | things is quite high. But it can be a very powerful and useful feature. |
|
1050 | 1027 | In a nutshell, you can redefine the way IPython processes the user input |
|
1051 | 1028 | line to accept new, special extensions to the syntax without needing to |
|
1052 | 1029 | change any of IPython's own code. |
|
1053 | 1030 | |
|
1054 | 1031 | In the IPython/extensions directory you will find some examples |
|
1055 | 1032 | supplied, which we will briefly describe now. These can be used 'as is' |
|
1056 | 1033 | (and both provide very useful functionality), or you can use them as a |
|
1057 | 1034 | starting point for writing your own extensions. |
|
1058 | 1035 | |
|
1059 | 1036 | .. _pasting_with_prompts: |
|
1060 | 1037 | |
|
1061 | 1038 | Pasting of code starting with Python or IPython prompts |
|
1062 | 1039 | ------------------------------------------------------- |
|
1063 | 1040 | |
|
1064 | 1041 | IPython is smart enough to filter out input prompts, be they plain Python ones |
|
1065 | 1042 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and `` ...:``). You can |
|
1066 | 1043 | therefore copy and paste from existing interactive sessions without worry. |
|
1067 | 1044 | |
|
1068 | 1045 | The following is a 'screenshot' of how things work, copying an example from the |
|
1069 | 1046 | standard Python tutorial:: |
|
1070 | 1047 | |
|
1071 | 1048 | In [1]: >>> # Fibonacci series: |
|
1072 | 1049 | |
|
1073 | 1050 | In [2]: ... # the sum of two elements defines the next |
|
1074 | 1051 | |
|
1075 | 1052 | In [3]: ... a, b = 0, 1 |
|
1076 | 1053 | |
|
1077 | 1054 | In [4]: >>> while b < 10: |
|
1078 | 1055 | ...: ... print b |
|
1079 | 1056 | ...: ... a, b = b, a+b |
|
1080 | 1057 | ...: |
|
1081 | 1058 | 1 |
|
1082 | 1059 | 1 |
|
1083 | 1060 | 2 |
|
1084 | 1061 | 3 |
|
1085 | 1062 | 5 |
|
1086 | 1063 | 8 |
|
1087 | 1064 | |
|
1088 | 1065 | And pasting from IPython sessions works equally well:: |
|
1089 | 1066 | |
|
1090 | 1067 | In [1]: In [5]: def f(x): |
|
1091 | 1068 | ...: ...: "A simple function" |
|
1092 | 1069 | ...: ...: return x**2 |
|
1093 | 1070 | ...: ...: |
|
1094 | 1071 | |
|
1095 | 1072 | In [2]: f(3) |
|
1096 | 1073 | Out[2]: 9 |
|
1097 | 1074 | |
|
1098 | 1075 | .. _gui_support: |
|
1099 | 1076 | |
|
1100 | 1077 | GUI event loop support |
|
1101 | 1078 | ====================== |
|
1102 | 1079 | |
|
1103 | 1080 | .. versionadded:: 0.11 |
|
1104 | 1081 | The ``%gui`` magic and :mod:`IPython.lib.inputhook`. |
|
1105 | 1082 | |
|
1106 | 1083 | IPython has excellent support for working interactively with Graphical User |
|
1107 | 1084 | Interface (GUI) toolkits, such as wxPython, PyQt4/PySide, PyGTK and Tk. This is |
|
1108 | 1085 | implemented using Python's builtin ``PyOSInputHook`` hook. This implementation |
|
1109 | 1086 | is extremely robust compared to our previous thread-based version. The |
|
1110 | 1087 | advantages of this are: |
|
1111 | 1088 | |
|
1112 | 1089 | * GUIs can be enabled and disabled dynamically at runtime. |
|
1113 | 1090 | * The active GUI can be switched dynamically at runtime. |
|
1114 | 1091 | * In some cases, multiple GUIs can run simultaneously with no problems. |
|
1115 | 1092 | * There is a developer API in :mod:`IPython.lib.inputhook` for customizing |
|
1116 | 1093 | all of these things. |
|
1117 | 1094 | |
|
1118 | 1095 | For users, enabling GUI event loop integration is simple. You simple use the |
|
1119 | 1096 | ``%gui`` magic as follows:: |
|
1120 | 1097 | |
|
1121 | 1098 | %gui [GUINAME] |
|
1122 | 1099 | |
|
1123 | 1100 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
1124 | 1101 | arguments are ``wx``, ``qt``, ``gtk`` and ``tk``. |
|
1125 | 1102 | |
|
1126 | 1103 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
1127 | 1104 | object, do:: |
|
1128 | 1105 | |
|
1129 | 1106 | %gui wx |
|
1130 | 1107 | |
|
1131 | 1108 | For information on IPython's Matplotlib integration (and the ``pylab`` mode) |
|
1132 | 1109 | see :ref:`this section <matplotlib_support>`. |
|
1133 | 1110 | |
|
1134 | 1111 | For developers that want to use IPython's GUI event loop integration in the |
|
1135 | 1112 | form of a library, these capabilities are exposed in library form in the |
|
1136 | 1113 | :mod:`IPython.lib.inputhook` and :mod:`IPython.lib.guisupport` modules. |
|
1137 | 1114 | Interested developers should see the module docstrings for more information, |
|
1138 | 1115 | but there are a few points that should be mentioned here. |
|
1139 | 1116 | |
|
1140 | 1117 | First, the ``PyOSInputHook`` approach only works in command line settings |
|
1141 | 1118 | where readline is activated. The integration with various eventloops |
|
1142 | 1119 | is handled somewhat differently (and more simply) when using the standalone |
|
1143 | 1120 | kernel, as in the qtconsole and notebook. |
|
1144 | 1121 | |
|
1145 | 1122 | Second, when using the ``PyOSInputHook`` approach, a GUI application should |
|
1146 | 1123 | *not* start its event loop. Instead all of this is handled by the |
|
1147 | 1124 | ``PyOSInputHook``. This means that applications that are meant to be used both |
|
1148 | 1125 | in IPython and as standalone apps need to have special code to detects how the |
|
1149 | 1126 | application is being run. We highly recommend using IPython's support for this. |
|
1150 | 1127 | Since the details vary slightly between toolkits, we point you to the various |
|
1151 | 1128 | examples in our source directory :file:`docs/examples/lib` that demonstrate |
|
1152 | 1129 | these capabilities. |
|
1153 | 1130 | |
|
1154 | 1131 | .. warning:: |
|
1155 | 1132 | |
|
1156 | 1133 | The WX version of this is currently broken. While ``--pylab=wx`` works |
|
1157 | 1134 | fine, standalone WX apps do not. See |
|
1158 | 1135 | https://github.com/ipython/ipython/issues/645 for details of our progress on |
|
1159 | 1136 | this issue. |
|
1160 | 1137 | |
|
1161 | 1138 | |
|
1162 | 1139 | Third, unlike previous versions of IPython, we no longer "hijack" (replace |
|
1163 | 1140 | them with no-ops) the event loops. This is done to allow applications that |
|
1164 | 1141 | actually need to run the real event loops to do so. This is often needed to |
|
1165 | 1142 | process pending events at critical points. |
|
1166 | 1143 | |
|
1167 | 1144 | Finally, we also have a number of examples in our source directory |
|
1168 | 1145 | :file:`docs/examples/lib` that demonstrate these capabilities. |
|
1169 | 1146 | |
|
1170 | 1147 | PyQt and PySide |
|
1171 | 1148 | --------------- |
|
1172 | 1149 | |
|
1173 | 1150 | .. attempt at explanation of the complete mess that is Qt support |
|
1174 | 1151 | |
|
1175 | 1152 | When you use ``--gui=qt`` or ``--pylab=qt``, IPython can work with either |
|
1176 | 1153 | PyQt4 or PySide. There are three options for configuration here, because |
|
1177 | 1154 | PyQt4 has two APIs for QString and QVariant - v1, which is the default on |
|
1178 | 1155 | Python 2, and the more natural v2, which is the only API supported by PySide. |
|
1179 | 1156 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole |
|
1180 | 1157 | uses v2, but you can still use any interface in your code, since the |
|
1181 | 1158 | Qt frontend is in a different process. |
|
1182 | 1159 | |
|
1183 | 1160 | The default will be to import PyQt4 without configuration of the APIs, thus |
|
1184 | 1161 | matching what most applications would expect. It will fall back of PySide if |
|
1185 | 1162 | PyQt4 is unavailable. |
|
1186 | 1163 | |
|
1187 | 1164 | If specified, IPython will respect the environment variable ``QT_API`` used |
|
1188 | 1165 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires |
|
1189 | 1166 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, |
|
1190 | 1167 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for |
|
1191 | 1168 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. |
|
1192 | 1169 | |
|
1193 | 1170 | If you launch IPython in pylab mode with ``ipython --pylab=qt``, then IPython |
|
1194 | 1171 | will ask matplotlib which Qt library to use (only if QT_API is *not set*), via |
|
1195 | 1172 | the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or older, then |
|
1196 | 1173 | IPython will always use PyQt4 without setting the v2 APIs, since neither v2 |
|
1197 | 1174 | PyQt nor PySide work. |
|
1198 | 1175 | |
|
1199 | 1176 | .. warning:: |
|
1200 | 1177 | |
|
1201 | 1178 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set |
|
1202 | 1179 | to work with IPython's qt integration, because otherwise PyQt4 will be |
|
1203 | 1180 | loaded in an incompatible mode. |
|
1204 | 1181 | |
|
1205 | 1182 | It also means that you must *not* have ``QT_API`` set if you want to |
|
1206 | 1183 | use ``--gui=qt`` with code that requires PyQt4 API v1. |
|
1207 | 1184 | |
|
1208 | 1185 | |
|
1209 | 1186 | .. _matplotlib_support: |
|
1210 | 1187 | |
|
1211 | 1188 | Plotting with matplotlib |
|
1212 | 1189 | ======================== |
|
1213 | 1190 | |
|
1214 | 1191 | `Matplotlib`_ provides high quality 2D and 3D plotting for Python. Matplotlib |
|
1215 | 1192 | can produce plots on screen using a variety of GUI toolkits, including Tk, |
|
1216 | 1193 | PyGTK, PyQt4 and wxPython. It also provides a number of commands useful for |
|
1217 | 1194 | scientific computing, all with a syntax compatible with that of the popular |
|
1218 | 1195 | Matlab program. |
|
1219 | 1196 | |
|
1220 | 1197 | To start IPython with matplotlib support, use the ``--pylab`` switch. If no |
|
1221 | 1198 | arguments are given, IPython will automatically detect your choice of |
|
1222 | 1199 | matplotlib backend. You can also request a specific backend with |
|
1223 | 1200 | ``--pylab=backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', 'gtk', |
|
1224 | 1201 | 'osx'. |
|
1225 | 1202 | |
|
1226 | 1203 | .. _Matplotlib: http://matplotlib.sourceforge.net |
|
1227 | 1204 | |
|
1228 | 1205 | .. _interactive_demos: |
|
1229 | 1206 | |
|
1230 | 1207 | Interactive demos with IPython |
|
1231 | 1208 | ============================== |
|
1232 | 1209 | |
|
1233 | 1210 | IPython ships with a basic system for running scripts interactively in |
|
1234 | 1211 | sections, useful when presenting code to audiences. A few tags embedded |
|
1235 | 1212 | in comments (so that the script remains valid Python code) divide a file |
|
1236 | 1213 | into separate blocks, and the demo can be run one block at a time, with |
|
1237 | 1214 | IPython printing (with syntax highlighting) the block before executing |
|
1238 | 1215 | it, and returning to the interactive prompt after each block. The |
|
1239 | 1216 | interactive namespace is updated after each block is run with the |
|
1240 | 1217 | contents of the demo's namespace. |
|
1241 | 1218 | |
|
1242 | 1219 | This allows you to show a piece of code, run it and then execute |
|
1243 | 1220 | interactively commands based on the variables just created. Once you |
|
1244 | 1221 | want to continue, you simply execute the next block of the demo. The |
|
1245 | 1222 | following listing shows the markup necessary for dividing a script into |
|
1246 | 1223 | sections for execution as a demo: |
|
1247 | 1224 | |
|
1248 | 1225 | .. literalinclude:: ../../examples/lib/example-demo.py |
|
1249 | 1226 | :language: python |
|
1250 | 1227 | |
|
1251 | 1228 | In order to run a file as a demo, you must first make a Demo object out |
|
1252 | 1229 | of it. If the file is named myscript.py, the following code will make a |
|
1253 | 1230 | demo:: |
|
1254 | 1231 | |
|
1255 | 1232 | from IPython.lib.demo import Demo |
|
1256 | 1233 | |
|
1257 | 1234 | mydemo = Demo('myscript.py') |
|
1258 | 1235 | |
|
1259 | 1236 | This creates the mydemo object, whose blocks you run one at a time by |
|
1260 | 1237 | simply calling the object with no arguments. If you have autocall active |
|
1261 | 1238 | in IPython (the default), all you need to do is type:: |
|
1262 | 1239 | |
|
1263 | 1240 | mydemo |
|
1264 | 1241 | |
|
1265 | 1242 | and IPython will call it, executing each block. Demo objects can be |
|
1266 | 1243 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1267 | 1244 | last block, etc. Simply use the Tab key on a demo object to see its |
|
1268 | 1245 | methods, and call '?' on them to see their docstrings for more usage |
|
1269 | 1246 | details. In addition, the demo module itself contains a comprehensive |
|
1270 | 1247 | docstring, which you can access via:: |
|
1271 | 1248 | |
|
1272 | 1249 | from IPython.lib import demo |
|
1273 | 1250 | |
|
1274 | 1251 | demo? |
|
1275 | 1252 | |
|
1276 | 1253 | Limitations: It is important to note that these demos are limited to |
|
1277 |
fairly simple uses. In particular, you can |
|
|
1254 | fairly simple uses. In particular, you cannot break up sections within | |
|
1278 | 1255 | indented code (loops, if statements, function definitions, etc.) |
|
1279 | 1256 | Supporting something like this would basically require tracking the |
|
1280 | 1257 | internal execution state of the Python interpreter, so only top-level |
|
1281 | 1258 | divisions are allowed. If you want to be able to open an IPython |
|
1282 | 1259 | instance at an arbitrary point in a program, you can use IPython's |
|
1283 | 1260 | embedding facilities, see :func:`IPython.embed` for details. |
|
1284 | 1261 |
General Comments 0
You need to be logged in to leave comments.
Login now