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