Show More
@@ -1,1579 +1,1578 b'' | |||||
1 | ================= |
|
1 | ================= | |
2 | IPython reference |
|
2 | IPython reference | |
3 | ================= |
|
3 | ================= | |
4 |
|
4 | |||
5 | .. warning:: |
|
5 | .. warning:: | |
6 |
|
6 | |||
7 | As of the 0.11 version of IPython, some of the features and APIs |
|
7 | As of the 0.11 version of IPython, some of the features and APIs | |
8 | described in this section have been deprecated or are broken. Our plan |
|
8 | described in this section have been deprecated or are broken. Our plan | |
9 | is to continue to support these features, but they need to be updated |
|
9 | is to continue to support these features, but they need to be updated | |
10 | to take advantage of recent API changes. Furthermore, this section |
|
10 | to take advantage of recent API changes. Furthermore, this section | |
11 | of the documentation need to be updated to reflect all of these changes. |
|
11 | of the documentation need to be updated to reflect all of these changes. | |
12 |
|
12 | |||
13 | .. _command_line_options: |
|
13 | .. _command_line_options: | |
14 |
|
14 | |||
15 | Command-line usage |
|
15 | Command-line usage | |
16 | ================== |
|
16 | ================== | |
17 |
|
17 | |||
18 | You start IPython with the command:: |
|
18 | You start IPython with the command:: | |
19 |
|
19 | |||
20 | $ ipython [options] files |
|
20 | $ ipython [options] files | |
21 |
|
21 | |||
22 | If invoked with no options, it executes all the files listed in sequence |
|
22 | If invoked with no options, it executes all the files listed in sequence | |
23 | and drops you into the interpreter while still acknowledging any options |
|
23 | and drops you into the interpreter while still acknowledging any options | |
24 | you may have set in your ipython_config.py. This behavior is different from |
|
24 | you may have set in your ipython_config.py. This behavior is different from | |
25 | standard Python, which when called as python -i will only execute one |
|
25 | standard Python, which when called as python -i will only execute one | |
26 | file and ignore your configuration setup. |
|
26 | file and ignore your configuration setup. | |
27 |
|
27 | |||
28 | Please note that some of the configuration options are not available at |
|
28 | Please note that some of the configuration options are not available at | |
29 | the command line, simply because they are not practical here. Look into |
|
29 | the command line, simply because they are not practical here. Look into | |
30 | your ipythonrc configuration file for details on those. This file is typically |
|
30 | your ipythonrc configuration file for details on those. This file is typically | |
31 | installed in the IPYTHON_DIR directory. For Linux |
|
31 | installed in the IPYTHON_DIR directory. For Linux | |
32 | users, this will be $HOME/.config/ipython, and for other users it will be |
|
32 | users, this will be $HOME/.config/ipython, and for other users it will be | |
33 | $HOME/.ipython. For Windows users, $HOME resolves to C:\\Documents and |
|
33 | $HOME/.ipython. For Windows users, $HOME resolves to C:\\Documents and | |
34 | Settings\\YourUserName in most instances. |
|
34 | Settings\\YourUserName in most instances. | |
35 |
|
35 | |||
36 |
|
36 | |||
37 |
|
37 | |||
38 |
|
38 | |||
39 | Special Threading Options |
|
39 | Special Threading Options | |
40 | ------------------------- |
|
40 | ------------------------- | |
41 |
|
41 | |||
42 | Previously IPython had command line options for controlling GUI event loop |
|
42 | Previously IPython had command line options for controlling GUI event loop | |
43 | integration (-gthread, -qthread, -q4thread, -wthread, -pylab). As of IPython |
|
43 | integration (-gthread, -qthread, -q4thread, -wthread, -pylab). As of IPython | |
44 | version 0.11, these have been removed. Please see the new ``%gui`` |
|
44 | version 0.11, these have been removed. Please see the new ``%gui`` | |
45 | magic command or :ref:`this section <gui_support>` for details on the new |
|
45 | magic command or :ref:`this section <gui_support>` for details on the new | |
46 | interface, or specify the gui at the commandline:: |
|
46 | interface, or specify the gui at the commandline:: | |
47 |
|
47 | |||
48 | $ ipython gui=qt |
|
48 | $ ipython gui=qt | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | Regular Options |
|
51 | Regular Options | |
52 | --------------- |
|
52 | --------------- | |
53 |
|
53 | |||
54 | After the above threading options have been given, regular options can |
|
54 | After the above threading options have been given, regular options can | |
55 | follow in any order. All options can be abbreviated to their shortest |
|
55 | follow in any order. All options can be abbreviated to their shortest | |
56 | non-ambiguous form and are case-sensitive. One or two dashes can be |
|
56 | non-ambiguous form and are case-sensitive. One or two dashes can be | |
57 | used. Some options have an alternate short form, indicated after a ``|``. |
|
57 | used. Some options have an alternate short form, indicated after a ``|``. | |
58 |
|
58 | |||
59 | Most options can also be set from your ipythonrc configuration file. See |
|
59 | Most options can also be set from your ipythonrc configuration file. See | |
60 | the provided example for more details on what the options do. Options |
|
60 | the provided example for more details on what the options do. Options | |
61 | given at the command line override the values set in the ipythonrc file. |
|
61 | given at the command line override the values set in the ipythonrc file. | |
62 |
|
62 | |||
63 | All options with a [no] prepended can be specified in negated form |
|
63 | All options with a [no] prepended can be specified in negated form | |
64 | (--no-option instead of --option) to turn the feature off. |
|
64 | (--no-option instead of --option) to turn the feature off. | |
65 |
|
65 | |||
66 | -h, --help print a help message and exit. |
|
66 | -h, --help print a help message and exit. | |
67 |
|
67 | |||
68 | --pylab, pylab=<name> |
|
68 | --pylab, pylab=<name> | |
69 | See :ref:`Matplotlib support <matplotlib_support>` |
|
69 | See :ref:`Matplotlib support <matplotlib_support>` | |
70 | for more details. |
|
70 | for more details. | |
71 |
|
71 | |||
72 | autocall=<val> |
|
72 | autocall=<val> | |
73 | Make IPython automatically call any callable object even if you |
|
73 | Make IPython automatically call any callable object even if you | |
74 | didn't type explicit parentheses. For example, 'str 43' becomes |
|
74 | didn't type explicit parentheses. For example, 'str 43' becomes | |
75 | 'str(43)' automatically. The value can be '0' to disable the feature, |
|
75 | 'str(43)' automatically. The value can be '0' to disable the feature, | |
76 | '1' for smart autocall, where it is not applied if there are no more |
|
76 | '1' for smart autocall, where it is not applied if there are no more | |
77 | arguments on the line, and '2' for full autocall, where all callable |
|
77 | arguments on the line, and '2' for full autocall, where all callable | |
78 | objects are automatically called (even if no arguments are |
|
78 | objects are automatically called (even if no arguments are | |
79 | present). The default is '1'. |
|
79 | present). The default is '1'. | |
80 |
|
80 | |||
81 | --[no-]autoindent |
|
81 | --[no-]autoindent | |
82 | Turn automatic indentation on/off. |
|
82 | Turn automatic indentation on/off. | |
83 |
|
83 | |||
84 | --[no-]automagic |
|
84 | --[no-]automagic | |
85 | make magic commands automatic (without needing their first character |
|
85 | make magic commands automatic (without needing their first character | |
86 | to be %). Type %magic at the IPython prompt for more information. |
|
86 | to be %). Type %magic at the IPython prompt for more information. | |
87 |
|
87 | |||
88 | --[no-]autoedit_syntax |
|
88 | --[no-]autoedit_syntax | |
89 | When a syntax error occurs after editing a file, automatically |
|
89 | When a syntax error occurs after editing a file, automatically | |
90 | open the file to the trouble causing line for convenient |
|
90 | open the file to the trouble causing line for convenient | |
91 | fixing. |
|
91 | fixing. | |
92 |
|
92 | |||
93 | --[no-]banner Print the initial information banner (default on). |
|
93 | --[no-]banner Print the initial information banner (default on). | |
94 |
|
94 | |||
95 | c=<command> |
|
95 | c=<command> | |
96 | execute the given command string. This is similar to the -c |
|
96 | execute the given command string. This is similar to the -c | |
97 | option in the normal Python interpreter. |
|
97 | option in the normal Python interpreter. | |
98 |
|
98 | |||
99 | cache_size=<n> |
|
99 | cache_size=<n> | |
100 | size of the output cache (maximum number of entries to hold in |
|
100 | size of the output cache (maximum number of entries to hold in | |
101 | memory). The default is 1000, you can change it permanently in your |
|
101 | memory). The default is 1000, you can change it permanently in your | |
102 | config file. Setting it to 0 completely disables the caching system, |
|
102 | config file. Setting it to 0 completely disables the caching system, | |
103 | and the minimum value accepted is 20 (if you provide a value less than |
|
103 | and the minimum value accepted is 20 (if you provide a value less than | |
104 | 20, it is reset to 0 and a warning is issued) This limit is defined |
|
104 | 20, it is reset to 0 and a warning is issued) This limit is defined | |
105 | because otherwise you'll spend more time re-flushing a too small cache |
|
105 | because otherwise you'll spend more time re-flushing a too small cache | |
106 | than working. |
|
106 | than working. | |
107 |
|
107 | |||
108 | --classic |
|
108 | --classic | |
109 | Gives IPython a similar feel to the classic Python |
|
109 | Gives IPython a similar feel to the classic Python | |
110 | prompt. |
|
110 | prompt. | |
111 |
|
111 | |||
112 | colors=<scheme> |
|
112 | colors=<scheme> | |
113 | Color scheme for prompts and exception reporting. Currently |
|
113 | Color scheme for prompts and exception reporting. Currently | |
114 | implemented: NoColor, Linux and LightBG. |
|
114 | implemented: NoColor, Linux and LightBG. | |
115 |
|
115 | |||
116 | --[no-]color_info |
|
116 | --[no-]color_info | |
117 | IPython can display information about objects via a set of functions, |
|
117 | IPython can display information about objects via a set of functions, | |
118 | and optionally can use colors for this, syntax highlighting source |
|
118 | and optionally can use colors for this, syntax highlighting source | |
119 | code and various other elements. However, because this information is |
|
119 | code and various other elements. However, because this information is | |
120 | passed through a pager (like 'less') and many pagers get confused with |
|
120 | passed through a pager (like 'less') and many pagers get confused with | |
121 | color codes, this option is off by default. You can test it and turn |
|
121 | color codes, this option is off by default. You can test it and turn | |
122 | it on permanently in your ipythonrc file if it works for you. As a |
|
122 | it on permanently in your ipythonrc file if it works for you. As a | |
123 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but |
|
123 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but | |
124 | that in RedHat 7.2 doesn't. |
|
124 | that in RedHat 7.2 doesn't. | |
125 |
|
125 | |||
126 | Test it and turn it on permanently if it works with your |
|
126 | Test it and turn it on permanently if it works with your | |
127 | system. The magic function %color_info allows you to toggle this |
|
127 | system. The magic function %color_info allows you to toggle this | |
128 | interactively for testing. |
|
128 | interactively for testing. | |
129 |
|
129 | |||
130 | --[no-]debug |
|
130 | --[no-]debug | |
131 | Show information about the loading process. Very useful to pin down |
|
131 | Show information about the loading process. Very useful to pin down | |
132 | problems with your configuration files or to get details about |
|
132 | problems with your configuration files or to get details about | |
133 | session restores. |
|
133 | session restores. | |
134 |
|
134 | |||
135 | --[no-]deep_reload: |
|
135 | --[no-]deep_reload: | |
136 | IPython can use the deep_reload module which reloads changes in |
|
136 | IPython can use the deep_reload module which reloads changes in | |
137 | modules recursively (it replaces the reload() function, so you don't |
|
137 | modules recursively (it replaces the reload() function, so you don't | |
138 | need to change anything to use it). deep_reload() forces a full |
|
138 | need to change anything to use it). deep_reload() forces a full | |
139 | reload of modules whose code may have changed, which the default |
|
139 | reload of modules whose code may have changed, which the default | |
140 | reload() function does not. |
|
140 | reload() function does not. | |
141 |
|
141 | |||
142 | When deep_reload is off, IPython will use the normal reload(), |
|
142 | When deep_reload is off, IPython will use the normal reload(), | |
143 | but deep_reload will still be available as dreload(). This |
|
143 | but deep_reload will still be available as dreload(). This | |
144 | feature is off by default [which means that you have both |
|
144 | feature is off by default [which means that you have both | |
145 | normal reload() and dreload()]. |
|
145 | normal reload() and dreload()]. | |
146 |
|
146 | |||
147 | editor=<name> |
|
147 | editor=<name> | |
148 | Which editor to use with the %edit command. By default, |
|
148 | Which editor to use with the %edit command. By default, | |
149 | IPython will honor your EDITOR environment variable (if not |
|
149 | IPython will honor your EDITOR environment variable (if not | |
150 | set, vi is the Unix default and notepad the Windows one). |
|
150 | set, vi is the Unix default and notepad the Windows one). | |
151 | Since this editor is invoked on the fly by IPython and is |
|
151 | Since this editor is invoked on the fly by IPython and is | |
152 | meant for editing small code snippets, you may want to use a |
|
152 | meant for editing small code snippets, you may want to use a | |
153 | small, lightweight editor here (in case your default EDITOR is |
|
153 | small, lightweight editor here (in case your default EDITOR is | |
154 | something like Emacs). |
|
154 | something like Emacs). | |
155 |
|
155 | |||
156 | ipython_dir=<name> |
|
156 | ipython_dir=<name> | |
157 | name of your IPython configuration directory IPYTHON_DIR. This |
|
157 | name of your IPython configuration directory IPYTHON_DIR. This | |
158 | can also be specified through the environment variable |
|
158 | can also be specified through the environment variable | |
159 | IPYTHON_DIR. |
|
159 | IPYTHON_DIR. | |
160 |
|
160 | |||
161 | -log, l |
|
161 | -log, l | |
162 | generate a log file of all input. The file is named |
|
162 | generate a log file of all input. The file is named | |
163 | ipython_log.py in your current directory (which prevents logs |
|
163 | ipython_log.py in your current directory (which prevents logs | |
164 | from multiple IPython sessions from trampling each other). You |
|
164 | from multiple IPython sessions from trampling each other). You | |
165 | can use this to later restore a session by loading your |
|
165 | can use this to later restore a session by loading your | |
166 | logfile as a file to be executed with option -logplay (see |
|
166 | logfile as a file to be executed with option -logplay (see | |
167 | below). |
|
167 | below). | |
168 |
|
168 | |||
169 | -logfile, lf <name> specify the name of your logfile. |
|
169 | -logfile, lf <name> specify the name of your logfile. | |
170 |
|
170 | |||
171 | -logplay, lp <name> |
|
171 | -logplay, lp <name> | |
172 |
|
172 | |||
173 | you can replay a previous log. For restoring a session as close as |
|
173 | you can replay a previous log. For restoring a session as close as | |
174 | possible to the state you left it in, use this option (don't just run |
|
174 | possible to the state you left it in, use this option (don't just run | |
175 | the logfile). With -logplay, IPython will try to reconstruct the |
|
175 | the logfile). With -logplay, IPython will try to reconstruct the | |
176 | previous working environment in full, not just execute the commands in |
|
176 | previous working environment in full, not just execute the commands in | |
177 | the logfile. |
|
177 | the logfile. | |
178 |
|
178 | |||
179 | When a session is restored, logging is automatically turned on |
|
179 | When a session is restored, logging is automatically turned on | |
180 | again with the name of the logfile it was invoked with (it is |
|
180 | again with the name of the logfile it was invoked with (it is | |
181 | read from the log header). So once you've turned logging on for |
|
181 | read from the log header). So once you've turned logging on for | |
182 | a session, you can quit IPython and reload it as many times as |
|
182 | a session, you can quit IPython and reload it as many times as | |
183 | you want and it will continue to log its history and restore |
|
183 | you want and it will continue to log its history and restore | |
184 | from the beginning every time. |
|
184 | from the beginning every time. | |
185 |
|
185 | |||
186 | Caveats: there are limitations in this option. The history |
|
186 | Caveats: there are limitations in this option. The history | |
187 | variables _i*,_* and _dh don't get restored properly. In the |
|
187 | variables _i*,_* and _dh don't get restored properly. In the | |
188 | future we will try to implement full session saving by writing |
|
188 | future we will try to implement full session saving by writing | |
189 | and retrieving a 'snapshot' of the memory state of IPython. But |
|
189 | and retrieving a 'snapshot' of the memory state of IPython. But | |
190 | our first attempts failed because of inherent limitations of |
|
190 | our first attempts failed because of inherent limitations of | |
191 | Python's Pickle module, so this may have to wait. |
|
191 | Python's Pickle module, so this may have to wait. | |
192 |
|
192 | |||
193 | --[no-]messages |
|
193 | --[no-]messages | |
194 | Print messages which IPython collects about its startup |
|
194 | Print messages which IPython collects about its startup | |
195 | process (default on). |
|
195 | process (default on). | |
196 |
|
196 | |||
197 | --[no-]pdb |
|
197 | --[no-]pdb | |
198 | Automatically call the pdb debugger after every uncaught |
|
198 | Automatically call the pdb debugger after every uncaught | |
199 | exception. If you are used to debugging using pdb, this puts |
|
199 | exception. If you are used to debugging using pdb, this puts | |
200 | you automatically inside of it after any call (either in |
|
200 | you automatically inside of it after any call (either in | |
201 | IPython or in code called by it) which triggers an exception |
|
201 | IPython or in code called by it) which triggers an exception | |
202 | which goes uncaught. |
|
202 | which goes uncaught. | |
203 |
|
203 | |||
204 | --pydb |
|
204 | --pydb | |
205 | Makes IPython use the third party "pydb" package as debugger, |
|
205 | Makes IPython use the third party "pydb" package as debugger, | |
206 | instead of pdb. Requires that pydb is installed. |
|
206 | instead of pdb. Requires that pydb is installed. | |
207 |
|
207 | |||
208 | --[no-]pprint |
|
208 | --[no-]pprint | |
209 | ipython can optionally use the pprint (pretty printer) module |
|
209 | ipython can optionally use the pprint (pretty printer) module | |
210 | for displaying results. pprint tends to give a nicer display |
|
210 | for displaying results. pprint tends to give a nicer display | |
211 | of nested data structures. If you like it, you can turn it on |
|
211 | of nested data structures. If you like it, you can turn it on | |
212 | permanently in your config file (default off). |
|
212 | permanently in your config file (default off). | |
213 |
|
213 | |||
214 | profile=<name> |
|
214 | profile=<name> | |
215 |
|
215 | |||
216 | assume that your config file is ipythonrc-<name> or |
|
216 | assume that your config file is ipythonrc-<name> or | |
217 | ipy_profile_<name>.py (looks in current dir first, then in |
|
217 | ipy_profile_<name>.py (looks in current dir first, then in | |
218 | IPYTHON_DIR). This is a quick way to keep and load multiple |
|
218 | IPYTHON_DIR). This is a quick way to keep and load multiple | |
219 | config files for different tasks, especially if you use the |
|
219 | config files for different tasks, especially if you use the | |
220 | include option of config files. You can keep a basic |
|
220 | include option of config files. You can keep a basic | |
221 | IPYTHON_DIR/ipythonrc file and then have other 'profiles' which |
|
221 | IPYTHON_DIR/ipythonrc file and then have other 'profiles' which | |
222 | include this one and load extra things for particular |
|
222 | include this one and load extra things for particular | |
223 | tasks. For example: |
|
223 | tasks. For example: | |
224 |
|
224 | |||
225 | 1. $IPYTHON_DIR/ipythonrc : load basic things you always want. |
|
225 | 1. $IPYTHON_DIR/ipythonrc : load basic things you always want. | |
226 | 2. $IPYTHON_DIR/ipythonrc-math : load (1) and basic math-related modules. |
|
226 | 2. $IPYTHON_DIR/ipythonrc-math : load (1) and basic math-related modules. | |
227 | 3. $IPYTHON_DIR/ipythonrc-numeric : load (1) and Numeric and plotting modules. |
|
227 | 3. $IPYTHON_DIR/ipythonrc-numeric : load (1) and Numeric and plotting modules. | |
228 |
|
228 | |||
229 | Since it is possible to create an endless loop by having |
|
229 | Since it is possible to create an endless loop by having | |
230 | circular file inclusions, IPython will stop if it reaches 15 |
|
230 | circular file inclusions, IPython will stop if it reaches 15 | |
231 | recursive inclusions. |
|
231 | recursive inclusions. | |
232 |
|
232 | |||
233 | pi1=<string> |
|
233 | pi1=<string> | |
234 |
|
234 | |||
235 | Specify the string used for input prompts. Note that if you are using |
|
235 | Specify the string used for input prompts. Note that if you are using | |
236 | numbered prompts, the number is represented with a '\#' in the |
|
236 | numbered prompts, the number is represented with a '\#' in the | |
237 | string. Don't forget to quote strings with spaces embedded in |
|
237 | string. Don't forget to quote strings with spaces embedded in | |
238 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` |
|
238 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` | |
239 | discusses in detail all the available escapes to customize your |
|
239 | discusses in detail all the available escapes to customize your | |
240 | prompts. |
|
240 | prompts. | |
241 |
|
241 | |||
242 | pi2=<string> |
|
242 | pi2=<string> | |
243 | Similar to the previous option, but used for the continuation |
|
243 | Similar to the previous option, but used for the continuation | |
244 | prompts. The special sequence '\D' is similar to '\#', but |
|
244 | prompts. The special sequence '\D' is similar to '\#', but | |
245 | with all digits replaced dots (so you can have your |
|
245 | with all digits replaced dots (so you can have your | |
246 | continuation prompt aligned with your input prompt). Default: |
|
246 | continuation prompt aligned with your input prompt). Default: | |
247 | ' .\D.:' (note three spaces at the start for alignment with |
|
247 | ' .\D.:' (note three spaces at the start for alignment with | |
248 | 'In [\#]'). |
|
248 | 'In [\#]'). | |
249 |
|
249 | |||
250 | po=<string> |
|
250 | po=<string> | |
251 | String used for output prompts, also uses numbers like |
|
251 | String used for output prompts, also uses numbers like | |
252 | prompt_in1. Default: 'Out[\#]:' |
|
252 | prompt_in1. Default: 'Out[\#]:' | |
253 |
|
253 | |||
254 | --quick |
|
254 | --quick | |
255 | start in bare bones mode (no config file loaded). |
|
255 | start in bare bones mode (no config file loaded). | |
256 |
|
256 | |||
257 | config_file=<name> |
|
257 | config_file=<name> | |
258 | name of your IPython resource configuration file. Normally |
|
258 | name of your IPython resource configuration file. Normally | |
259 | IPython loads ipython_config.py (from current directory) or |
|
259 | IPython loads ipython_config.py (from current directory) or | |
260 | IPYTHON_DIR/profile_default. |
|
260 | IPYTHON_DIR/profile_default. | |
261 |
|
261 | |||
262 | If the loading of your config file fails, IPython starts with |
|
262 | If the loading of your config file fails, IPython starts with | |
263 | a bare bones configuration (no modules loaded at all). |
|
263 | a bare bones configuration (no modules loaded at all). | |
264 |
|
264 | |||
265 | --[no-]readline |
|
265 | --[no-]readline | |
266 | use the readline library, which is needed to support name |
|
266 | use the readline library, which is needed to support name | |
267 | completion and command history, among other things. It is |
|
267 | completion and command history, among other things. It is | |
268 | enabled by default, but may cause problems for users of |
|
268 | enabled by default, but may cause problems for users of | |
269 | X/Emacs in Python comint or shell buffers. |
|
269 | X/Emacs in Python comint or shell buffers. | |
270 |
|
270 | |||
271 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support |
|
271 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support | |
272 | IPython's readline and syntax coloring fine, only 'emacs' (M-x |
|
272 | IPython's readline and syntax coloring fine, only 'emacs' (M-x | |
273 | shell and C-c !) buffers do not. |
|
273 | shell and C-c !) buffers do not. | |
274 |
|
274 | |||
275 | sl=<n> |
|
275 | sl=<n> | |
276 | number of lines of your screen. This is used to control |
|
276 | number of lines of your screen. This is used to control | |
277 | printing of very long strings. Strings longer than this number |
|
277 | printing of very long strings. Strings longer than this number | |
278 | of lines will be sent through a pager instead of directly |
|
278 | of lines will be sent through a pager instead of directly | |
279 | printed. |
|
279 | printed. | |
280 |
|
280 | |||
281 | The default value for this is 0, which means IPython will |
|
281 | The default value for this is 0, which means IPython will | |
282 | auto-detect your screen size every time it needs to print certain |
|
282 | auto-detect your screen size every time it needs to print certain | |
283 | potentially long strings (this doesn't change the behavior of the |
|
283 | potentially long strings (this doesn't change the behavior of the | |
284 | 'print' keyword, it's only triggered internally). If for some |
|
284 | 'print' keyword, it's only triggered internally). If for some | |
285 | reason this isn't working well (it needs curses support), specify |
|
285 | reason this isn't working well (it needs curses support), specify | |
286 | it yourself. Otherwise don't change the default. |
|
286 | it yourself. Otherwise don't change the default. | |
287 |
|
287 | |||
288 | si=<string> |
|
288 | si=<string> | |
289 |
|
289 | |||
290 | separator before input prompts. |
|
290 | separator before input prompts. | |
291 | Default: '\n' |
|
291 | Default: '\n' | |
292 |
|
292 | |||
293 | so=<string> |
|
293 | so=<string> | |
294 | separator before output prompts. |
|
294 | separator before output prompts. | |
295 | Default: nothing. |
|
295 | Default: nothing. | |
296 |
|
296 | |||
297 | so2=<string> |
|
297 | so2=<string> | |
298 | separator after output prompts. |
|
298 | separator after output prompts. | |
299 | Default: nothing. |
|
299 | Default: nothing. | |
300 | For these three options, use the value 0 to specify no separator. |
|
300 | For these three options, use the value 0 to specify no separator. | |
301 |
|
301 | |||
302 | --nosep |
|
302 | --nosep | |
303 | shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 |
|
303 | shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 | |
304 | 0'. Simply removes all input/output separators. |
|
304 | 0'. Simply removes all input/output separators. | |
305 |
|
305 | |||
306 | --init |
|
306 | --init | |
307 | allows you to initialize your IPYTHON_DIR configuration when you |
|
307 | allows you to initialize your IPYTHON_DIR configuration when you | |
308 | install a new version of IPython. Since new versions may |
|
308 | install a new version of IPython. Since new versions may | |
309 | include new command line options or example files, this copies |
|
309 | include new command line options or example files, this copies | |
310 | updated config files. However, it backs up (with a |
|
310 | updated config files. However, it backs up (with a | |
311 | .old extension) all files which it overwrites so that you can |
|
311 | .old extension) all files which it overwrites so that you can | |
312 | merge back any customizations you might have in your personal |
|
312 | merge back any customizations you might have in your personal | |
313 | files. Note that you should probably use %upgrade instead, |
|
313 | files. Note that you should probably use %upgrade instead, | |
314 | it's a safer alternative. |
|
314 | it's a safer alternative. | |
315 |
|
315 | |||
316 |
|
316 | |||
317 | --version print version information and exit. |
|
317 | --version print version information and exit. | |
318 |
|
318 | |||
319 | xmode=<modename> |
|
319 | xmode=<modename> | |
320 |
|
320 | |||
321 | Mode for exception reporting. |
|
321 | Mode for exception reporting. | |
322 |
|
322 | |||
323 | Valid modes: Plain, Context and Verbose. |
|
323 | Valid modes: Plain, Context and Verbose. | |
324 |
|
324 | |||
325 | * Plain: similar to python's normal traceback printing. |
|
325 | * Plain: similar to python's normal traceback printing. | |
326 | * Context: prints 5 lines of context source code around each |
|
326 | * Context: prints 5 lines of context source code around each | |
327 | line in the traceback. |
|
327 | line in the traceback. | |
328 | * Verbose: similar to Context, but additionally prints the |
|
328 | * Verbose: similar to Context, but additionally prints the | |
329 | variables currently visible where the exception happened |
|
329 | variables currently visible where the exception happened | |
330 | (shortening their strings if too long). This can potentially be |
|
330 | (shortening their strings if too long). This can potentially be | |
331 | very slow, if you happen to have a huge data structure whose |
|
331 | very slow, if you happen to have a huge data structure whose | |
332 | string representation is complex to compute. Your computer may |
|
332 | string representation is complex to compute. Your computer may | |
333 | appear to freeze for a while with cpu usage at 100%. If this |
|
333 | appear to freeze for a while with cpu usage at 100%. If this | |
334 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it |
|
334 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it | |
335 | more than once). |
|
335 | more than once). | |
336 |
|
336 | |||
337 | Interactive use |
|
337 | Interactive use | |
338 | =============== |
|
338 | =============== | |
339 |
|
339 | |||
340 | Warning: IPython relies on the existence of a global variable called |
|
340 | Warning: IPython relies on the existence of a global variable called | |
341 | _ip which controls the shell itself. If you redefine _ip to anything, |
|
341 | _ip which controls the shell itself. If you redefine _ip to anything, | |
342 | bizarre behavior will quickly occur. |
|
342 | bizarre behavior will quickly occur. | |
343 |
|
343 | |||
344 | Other than the above warning, IPython is meant to work as a drop-in |
|
344 | Other than the above warning, IPython is meant to work as a drop-in | |
345 | replacement for the standard interactive interpreter. As such, any code |
|
345 | replacement for the standard interactive interpreter. As such, any code | |
346 | which is valid python should execute normally under IPython (cases where |
|
346 | which is valid python should execute normally under IPython (cases where | |
347 | this is not true should be reported as bugs). It does, however, offer |
|
347 | this is not true should be reported as bugs). It does, however, offer | |
348 | many features which are not available at a standard python prompt. What |
|
348 | many features which are not available at a standard python prompt. What | |
349 | follows is a list of these. |
|
349 | follows is a list of these. | |
350 |
|
350 | |||
351 |
|
351 | |||
352 | Caution for Windows users |
|
352 | Caution for Windows users | |
353 | ------------------------- |
|
353 | ------------------------- | |
354 |
|
354 | |||
355 | Windows, unfortunately, uses the '\' character as a path |
|
355 | Windows, unfortunately, uses the '\\' character as a path | |
356 | separator. This is a terrible choice, because '\' also represents the |
|
356 | separator. This is a terrible choice, because '\\' also represents the | |
357 | escape character in most modern programming languages, including |
|
357 | escape character in most modern programming languages, including | |
358 | Python. For this reason, using '/' character is recommended if you |
|
358 | Python. For this reason, using '/' character is recommended if you | |
359 | have problems with ``\``. However, in Windows commands '/' flags |
|
359 | have problems with ``\``. However, in Windows commands '/' flags | |
360 | options, so you can not use it for the root directory. This means that |
|
360 | options, so you can not use it for the root directory. This means that | |
361 | paths beginning at the root must be typed in a contrived manner like: |
|
361 | paths beginning at the root must be typed in a contrived manner like: | |
362 | ``%copy \opt/foo/bar.txt \tmp`` |
|
362 | ``%copy \opt/foo/bar.txt \tmp`` | |
363 |
|
363 | |||
364 | .. _magic: |
|
364 | .. _magic: | |
365 |
|
365 | |||
366 | Magic command system |
|
366 | Magic command system | |
367 | -------------------- |
|
367 | -------------------- | |
368 |
|
368 | |||
369 | IPython will treat any line whose first character is a % as a special |
|
369 | IPython will treat any line whose first character is a % as a special | |
370 | call to a 'magic' function. These allow you to control the behavior of |
|
370 | call to a 'magic' function. These allow you to control the behavior of | |
371 | IPython itself, plus a lot of system-type features. They are all |
|
371 | IPython itself, plus a lot of system-type features. They are all | |
372 | prefixed with a % character, but parameters are given without |
|
372 | prefixed with a % character, but parameters are given without | |
373 | parentheses or quotes. |
|
373 | parentheses or quotes. | |
374 |
|
374 | |||
375 |
Example: typing |
|
375 | Example: typing ``%cd mydir`` changes your working directory to 'mydir', if it | |
376 | directory to 'mydir', if it exists. |
|
376 | exists. | |
377 |
|
377 | |||
378 |
If you have 'automagic' enabled ( |
|
378 | If you have 'automagic' enabled (as it by default), you don't need | |
379 | line option -automagic or with the %automagic function), you don't need |
|
|||
380 | to type in the % explicitly. IPython will scan its internal list of |
|
379 | to type in the % explicitly. IPython will scan its internal list of | |
381 | magic functions and call one if it exists. With automagic on you can |
|
380 | magic functions and call one if it exists. With automagic on you can | |
382 |
then just type |
|
381 | then just type ``cd mydir`` to go to directory 'mydir'. The automagic | |
383 | system has the lowest possible precedence in name searches, so defining |
|
382 | system has the lowest possible precedence in name searches, so defining | |
384 | an identifier with the same name as an existing magic function will |
|
383 | an identifier with the same name as an existing magic function will | |
385 | shadow it for automagic use. You can still access the shadowed magic |
|
384 | shadow it for automagic use. You can still access the shadowed magic | |
386 | function by explicitly using the % character at the beginning of the line. |
|
385 | function by explicitly using the % character at the beginning of the line. | |
387 |
|
386 | |||
388 | An example (with automagic on) should clarify all this:: |
|
387 | An example (with automagic on) should clarify all this:: | |
389 |
|
388 | |||
390 | In [1]: cd ipython # %cd is called by automagic |
|
389 | In [1]: cd ipython # %cd is called by automagic | |
391 |
|
390 | |||
392 | /home/fperez/ipython |
|
391 | /home/fperez/ipython | |
393 |
|
392 | |||
394 | In [2]: cd=1 # now cd is just a variable |
|
393 | In [2]: cd=1 # now cd is just a variable | |
395 |
|
394 | |||
396 | In [3]: cd .. # and doesn't work as a function anymore |
|
395 | In [3]: cd .. # and doesn't work as a function anymore | |
397 |
|
396 | |||
398 | ------------------------------ |
|
397 | ------------------------------ | |
399 |
|
398 | |||
400 | File "<console>", line 1 |
|
399 | File "<console>", line 1 | |
401 |
|
400 | |||
402 | cd .. |
|
401 | cd .. | |
403 |
|
402 | |||
404 | ^ |
|
403 | ^ | |
405 |
|
404 | |||
406 | SyntaxError: invalid syntax |
|
405 | SyntaxError: invalid syntax | |
407 |
|
406 | |||
408 | In [4]: %cd .. # but %cd always works |
|
407 | In [4]: %cd .. # but %cd always works | |
409 |
|
408 | |||
410 | /home/fperez |
|
409 | /home/fperez | |
411 |
|
410 | |||
412 | In [5]: del cd # if you remove the cd variable |
|
411 | In [5]: del cd # if you remove the cd variable | |
413 |
|
412 | |||
414 | In [6]: cd ipython # automagic can work again |
|
413 | In [6]: cd ipython # automagic can work again | |
415 |
|
414 | |||
416 | /home/fperez/ipython |
|
415 | /home/fperez/ipython | |
417 |
|
416 | |||
418 | You can define your own magic functions to extend the system. The |
|
417 | You can define your own magic functions to extend the system. The | |
419 | following example defines a new magic command, %impall:: |
|
418 | following example defines a new magic command, %impall:: | |
420 |
|
419 | |||
421 | import IPython.ipapi |
|
420 | import IPython.ipapi | |
422 |
|
421 | |||
423 | ip = IPython.ipapi.get() |
|
422 | ip = IPython.ipapi.get() | |
424 |
|
423 | |||
425 | def doimp(self, arg): |
|
424 | def doimp(self, arg): | |
426 |
|
425 | |||
427 | ip = self.api |
|
426 | ip = self.api | |
428 |
|
427 | |||
429 | ip.ex("import %s; reload(%s); from %s import *" % ( |
|
428 | ip.ex("import %s; reload(%s); from %s import *" % ( | |
430 |
|
429 | |||
431 | arg,arg,arg) |
|
430 | arg,arg,arg) | |
432 |
|
431 | |||
433 | ) |
|
432 | ) | |
434 |
|
433 | |||
435 | ip.expose_magic('impall', doimp) |
|
434 | ip.expose_magic('impall', doimp) | |
436 |
|
435 | |||
437 | You can also define your own aliased names for magic functions. In your |
|
436 | You can also define your own aliased names for magic functions. In your | |
438 | ipythonrc file, placing a line like:: |
|
437 | ipythonrc file, placing a line like:: | |
439 |
|
438 | |||
440 | execute __IP.magic_cl = __IP.magic_clear |
|
439 | execute __IP.magic_cl = __IP.magic_clear | |
441 |
|
440 | |||
442 | will define %cl as a new name for %clear. |
|
441 | will define %cl as a new name for %clear. | |
443 |
|
442 | |||
444 | Type %magic for more information, including a list of all available |
|
443 | Type %magic for more information, including a list of all available | |
445 | magic functions at any time and their docstrings. You can also type |
|
444 | magic functions at any time and their docstrings. You can also type | |
446 | %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for |
|
445 | %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for | |
447 | information on the '?' system) to get information about any particular |
|
446 | information on the '?' system) to get information about any particular | |
448 | magic function you are interested in. |
|
447 | magic function you are interested in. | |
449 |
|
448 | |||
450 | The API documentation for the :mod:`IPython.Magic` module contains the full |
|
449 | The API documentation for the :mod:`IPython.Magic` module contains the full | |
451 | docstrings of all currently available magic commands. |
|
450 | docstrings of all currently available magic commands. | |
452 |
|
451 | |||
453 |
|
452 | |||
454 | Access to the standard Python help |
|
453 | Access to the standard Python help | |
455 | ---------------------------------- |
|
454 | ---------------------------------- | |
456 |
|
455 | |||
457 | As of Python 2.1, a help system is available with access to object docstrings |
|
456 | As of Python 2.1, a help system is available with access to object docstrings | |
458 | and the Python manuals. Simply type 'help' (no quotes) to access it. You can |
|
457 | and the Python manuals. Simply type 'help' (no quotes) to access it. You can | |
459 | also type help(object) to obtain information about a given object, and |
|
458 | also type help(object) to obtain information about a given object, and | |
460 | help('keyword') for information on a keyword. As noted :ref:`here |
|
459 | help('keyword') for information on a keyword. As noted :ref:`here | |
461 | <accessing_help>`, you need to properly configure your environment variable |
|
460 | <accessing_help>`, you need to properly configure your environment variable | |
462 | PYTHONDOCS for this feature to work correctly. |
|
461 | PYTHONDOCS for this feature to work correctly. | |
463 |
|
462 | |||
464 | .. _dynamic_object_info: |
|
463 | .. _dynamic_object_info: | |
465 |
|
464 | |||
466 | Dynamic object information |
|
465 | Dynamic object information | |
467 | -------------------------- |
|
466 | -------------------------- | |
468 |
|
467 | |||
469 | Typing ?word or word? prints detailed information about an object. If |
|
468 | Typing ?word or word? prints detailed information about an object. If | |
470 | certain strings in the object are too long (docstrings, code, etc.) they |
|
469 | certain strings in the object are too long (docstrings, code, etc.) they | |
471 | get snipped in the center for brevity. This system gives access variable |
|
470 | get snipped in the center for brevity. This system gives access variable | |
472 | types and values, full source code for any object (if available), |
|
471 | types and values, full source code for any object (if available), | |
473 | function prototypes and other useful information. |
|
472 | function prototypes and other useful information. | |
474 |
|
473 | |||
475 | Typing ??word or word?? gives access to the full information without |
|
474 | Typing ??word or word?? gives access to the full information without | |
476 | snipping long strings. Long strings are sent to the screen through the |
|
475 | snipping long strings. Long strings are sent to the screen through the | |
477 | less pager if longer than the screen and printed otherwise. On systems |
|
476 | less pager if longer than the screen and printed otherwise. On systems | |
478 | lacking the less command, IPython uses a very basic internal pager. |
|
477 | lacking the less command, IPython uses a very basic internal pager. | |
479 |
|
478 | |||
480 | The following magic functions are particularly useful for gathering |
|
479 | The following magic functions are particularly useful for gathering | |
481 | information about your working environment. You can get more details by |
|
480 | information about your working environment. You can get more details by | |
482 | typing %magic or querying them individually (use %function_name? with or |
|
481 | typing %magic or querying them individually (use %function_name? with or | |
483 | without the %), this is just a summary: |
|
482 | without the %), this is just a summary: | |
484 |
|
483 | |||
485 | * **%pdoc <object>**: Print (or run through a pager if too long) the |
|
484 | * **%pdoc <object>**: Print (or run through a pager if too long) the | |
486 | docstring for an object. If the given object is a class, it will |
|
485 | docstring for an object. If the given object is a class, it will | |
487 | print both the class and the constructor docstrings. |
|
486 | print both the class and the constructor docstrings. | |
488 | * **%pdef <object>**: Print the definition header for any callable |
|
487 | * **%pdef <object>**: Print the definition header for any callable | |
489 | object. If the object is a class, print the constructor information. |
|
488 | object. If the object is a class, print the constructor information. | |
490 | * **%psource <object>**: Print (or run through a pager if too long) |
|
489 | * **%psource <object>**: Print (or run through a pager if too long) | |
491 | the source code for an object. |
|
490 | the source code for an object. | |
492 | * **%pfile <object>**: Show the entire source file where an object was |
|
491 | * **%pfile <object>**: Show the entire source file where an object was | |
493 | defined via a pager, opening it at the line where the object |
|
492 | defined via a pager, opening it at the line where the object | |
494 | definition begins. |
|
493 | definition begins. | |
495 | * **%who/%whos**: These functions give information about identifiers |
|
494 | * **%who/%whos**: These functions give information about identifiers | |
496 | you have defined interactively (not things you loaded or defined |
|
495 | you have defined interactively (not things you loaded or defined | |
497 | in your configuration files). %who just prints a list of |
|
496 | in your configuration files). %who just prints a list of | |
498 | identifiers and %whos prints a table with some basic details about |
|
497 | identifiers and %whos prints a table with some basic details about | |
499 | each identifier. |
|
498 | each identifier. | |
500 |
|
499 | |||
501 | Note that the dynamic object information functions (?/??, %pdoc, %pfile, |
|
500 | Note that the dynamic object information functions (?/??, %pdoc, %pfile, | |
502 | %pdef, %psource) give you access to documentation even on things which |
|
501 | %pdef, %psource) give you access to documentation even on things which | |
503 | are not really defined as separate identifiers. Try for example typing |
|
502 | are not really defined as separate identifiers. Try for example typing | |
504 | {}.get? or after doing import os, type os.path.abspath??. |
|
503 | {}.get? or after doing import os, type os.path.abspath??. | |
505 |
|
504 | |||
506 |
|
505 | |||
507 | .. _readline: |
|
506 | .. _readline: | |
508 |
|
507 | |||
509 | Readline-based features |
|
508 | Readline-based features | |
510 | ----------------------- |
|
509 | ----------------------- | |
511 |
|
510 | |||
512 | These features require the GNU readline library, so they won't work if |
|
511 | These features require the GNU readline library, so they won't work if | |
513 | your Python installation lacks readline support. We will first describe |
|
512 | your Python installation lacks readline support. We will first describe | |
514 | the default behavior IPython uses, and then how to change it to suit |
|
513 | the default behavior IPython uses, and then how to change it to suit | |
515 | your preferences. |
|
514 | your preferences. | |
516 |
|
515 | |||
517 |
|
516 | |||
518 | Command line completion |
|
517 | Command line completion | |
519 | +++++++++++++++++++++++ |
|
518 | +++++++++++++++++++++++ | |
520 |
|
519 | |||
521 | At any time, hitting TAB will complete any available python commands or |
|
520 | At any time, hitting TAB will complete any available python commands or | |
522 | variable names, and show you a list of the possible completions if |
|
521 | variable names, and show you a list of the possible completions if | |
523 | there's no unambiguous one. It will also complete filenames in the |
|
522 | there's no unambiguous one. It will also complete filenames in the | |
524 | current directory if no python names match what you've typed so far. |
|
523 | current directory if no python names match what you've typed so far. | |
525 |
|
524 | |||
526 |
|
525 | |||
527 | Search command history |
|
526 | Search command history | |
528 | ++++++++++++++++++++++ |
|
527 | ++++++++++++++++++++++ | |
529 |
|
528 | |||
530 | IPython provides two ways for searching through previous input and thus |
|
529 | IPython provides two ways for searching through previous input and thus | |
531 | reduce the need for repetitive typing: |
|
530 | reduce the need for repetitive typing: | |
532 |
|
531 | |||
533 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n |
|
532 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n | |
534 | (next,down) to search through only the history items that match |
|
533 | (next,down) to search through only the history items that match | |
535 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank |
|
534 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank | |
536 | prompt, they just behave like normal arrow keys. |
|
535 | prompt, they just behave like normal arrow keys. | |
537 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system |
|
536 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system | |
538 | searches your history for lines that contain what you've typed so |
|
537 | searches your history for lines that contain what you've typed so | |
539 | far, completing as much as it can. |
|
538 | far, completing as much as it can. | |
540 |
|
539 | |||
541 |
|
540 | |||
542 | Persistent command history across sessions |
|
541 | Persistent command history across sessions | |
543 | ++++++++++++++++++++++++++++++++++++++++++ |
|
542 | ++++++++++++++++++++++++++++++++++++++++++ | |
544 |
|
543 | |||
545 | IPython will save your input history when it leaves and reload it next |
|
544 | IPython will save your input history when it leaves and reload it next | |
546 | time you restart it. By default, the history file is named |
|
545 | time you restart it. By default, the history file is named | |
547 | $IPYTHON_DIR/history, but if you've loaded a named profile, |
|
546 | $IPYTHON_DIR/history, but if you've loaded a named profile, | |
548 | '-PROFILE_NAME' is appended to the name. This allows you to keep |
|
547 | '-PROFILE_NAME' is appended to the name. This allows you to keep | |
549 | separate histories related to various tasks: commands related to |
|
548 | separate histories related to various tasks: commands related to | |
550 | numerical work will not be clobbered by a system shell history, for |
|
549 | numerical work will not be clobbered by a system shell history, for | |
551 | example. |
|
550 | example. | |
552 |
|
551 | |||
553 |
|
552 | |||
554 | Autoindent |
|
553 | Autoindent | |
555 | ++++++++++ |
|
554 | ++++++++++ | |
556 |
|
555 | |||
557 | IPython can recognize lines ending in ':' and indent the next line, |
|
556 | IPython can recognize lines ending in ':' and indent the next line, | |
558 | while also un-indenting automatically after 'raise' or 'return'. |
|
557 | while also un-indenting automatically after 'raise' or 'return'. | |
559 |
|
558 | |||
560 | This feature uses the readline library, so it will honor your ~/.inputrc |
|
559 | This feature uses the readline library, so it will honor your ~/.inputrc | |
561 | configuration (or whatever file your INPUTRC variable points to). Adding |
|
560 | configuration (or whatever file your INPUTRC variable points to). Adding | |
562 | the following lines to your .inputrc file can make indenting/unindenting |
|
561 | the following lines to your .inputrc file can make indenting/unindenting | |
563 | more convenient (M-i indents, M-u unindents):: |
|
562 | more convenient (M-i indents, M-u unindents):: | |
564 |
|
563 | |||
565 | $if Python |
|
564 | $if Python | |
566 | "\M-i": " " |
|
565 | "\M-i": " " | |
567 | "\M-u": "\d\d\d\d" |
|
566 | "\M-u": "\d\d\d\d" | |
568 | $endif |
|
567 | $endif | |
569 |
|
568 | |||
570 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
569 | Note that there are 4 spaces between the quote marks after "M-i" above. | |
571 |
|
570 | |||
572 | Warning: this feature is ON by default, but it can cause problems with |
|
571 | Warning: this feature is ON by default, but it can cause problems with | |
573 | the pasting of multi-line indented code (the pasted code gets |
|
572 | the pasting of multi-line indented code (the pasted code gets | |
574 | re-indented on each line). A magic function %autoindent allows you to |
|
573 | re-indented on each line). A magic function %autoindent allows you to | |
575 | toggle it on/off at runtime. You can also disable it permanently on in |
|
574 | toggle it on/off at runtime. You can also disable it permanently on in | |
576 | your ipythonrc file (set autoindent 0). |
|
575 | your ipythonrc file (set autoindent 0). | |
577 |
|
576 | |||
578 |
|
577 | |||
579 | Customizing readline behavior |
|
578 | Customizing readline behavior | |
580 | +++++++++++++++++++++++++++++ |
|
579 | +++++++++++++++++++++++++++++ | |
581 |
|
580 | |||
582 | All these features are based on the GNU readline library, which has an |
|
581 | All these features are based on the GNU readline library, which has an | |
583 | extremely customizable interface. Normally, readline is configured via a |
|
582 | extremely customizable interface. Normally, readline is configured via a | |
584 | file which defines the behavior of the library; the details of the |
|
583 | file which defines the behavior of the library; the details of the | |
585 | syntax for this can be found in the readline documentation available |
|
584 | syntax for this can be found in the readline documentation available | |
586 | with your system or on the Internet. IPython doesn't read this file (if |
|
585 | with your system or on the Internet. IPython doesn't read this file (if | |
587 | it exists) directly, but it does support passing to readline valid |
|
586 | it exists) directly, but it does support passing to readline valid | |
588 | options via a simple interface. In brief, you can customize readline by |
|
587 | options via a simple interface. In brief, you can customize readline by | |
589 | setting the following options in your ipythonrc configuration file (note |
|
588 | setting the following options in your ipythonrc configuration file (note | |
590 | that these options can not be specified at the command line): |
|
589 | that these options can not be specified at the command line): | |
591 |
|
590 | |||
592 | * **readline_parse_and_bind**: this option can appear as many times as |
|
591 | * **readline_parse_and_bind**: this option can appear as many times as | |
593 | you want, each time defining a string to be executed via a |
|
592 | you want, each time defining a string to be executed via a | |
594 | readline.parse_and_bind() command. The syntax for valid commands |
|
593 | readline.parse_and_bind() command. The syntax for valid commands | |
595 | of this kind can be found by reading the documentation for the GNU |
|
594 | of this kind can be found by reading the documentation for the GNU | |
596 | readline library, as these commands are of the kind which readline |
|
595 | readline library, as these commands are of the kind which readline | |
597 | accepts in its configuration file. |
|
596 | accepts in its configuration file. | |
598 | * **readline_remove_delims**: a string of characters to be removed |
|
597 | * **readline_remove_delims**: a string of characters to be removed | |
599 | from the default word-delimiters list used by readline, so that |
|
598 | from the default word-delimiters list used by readline, so that | |
600 | completions may be performed on strings which contain them. Do not |
|
599 | completions may be performed on strings which contain them. Do not | |
601 | change the default value unless you know what you're doing. |
|
600 | change the default value unless you know what you're doing. | |
602 | * **readline_omit__names**: when tab-completion is enabled, hitting |
|
601 | * **readline_omit__names**: when tab-completion is enabled, hitting | |
603 | <tab> after a '.' in a name will complete all attributes of an |
|
602 | <tab> after a '.' in a name will complete all attributes of an | |
604 | object, including all the special methods whose names include |
|
603 | object, including all the special methods whose names include | |
605 | double underscores (like __getitem__ or __class__). If you'd |
|
604 | double underscores (like __getitem__ or __class__). If you'd | |
606 | rather not see these names by default, you can set this option to |
|
605 | rather not see these names by default, you can set this option to | |
607 | 1. Note that even when this option is set, you can still see those |
|
606 | 1. Note that even when this option is set, you can still see those | |
608 | names by explicitly typing a _ after the period and hitting <tab>: |
|
607 | names by explicitly typing a _ after the period and hitting <tab>: | |
609 | 'name._<tab>' will always complete attribute names starting with '_'. |
|
608 | 'name._<tab>' will always complete attribute names starting with '_'. | |
610 |
|
609 | |||
611 | This option is off by default so that new users see all |
|
610 | This option is off by default so that new users see all | |
612 | attributes of any objects they are dealing with. |
|
611 | attributes of any objects they are dealing with. | |
613 |
|
612 | |||
614 | You will find the default values along with a corresponding detailed |
|
613 | You will find the default values along with a corresponding detailed | |
615 | explanation in your ipythonrc file. |
|
614 | explanation in your ipythonrc file. | |
616 |
|
615 | |||
617 |
|
616 | |||
618 | Session logging and restoring |
|
617 | Session logging and restoring | |
619 | ----------------------------- |
|
618 | ----------------------------- | |
620 |
|
619 | |||
621 | You can log all input from a session either by starting IPython with the |
|
620 | You can log all input from a session either by starting IPython with the | |
622 | command line switches -log or -logfile (see :ref:`here <command_line_options>`) |
|
621 | command line switches -log or -logfile (see :ref:`here <command_line_options>`) | |
623 | or by activating the logging at any moment with the magic function %logstart. |
|
622 | or by activating the logging at any moment with the magic function %logstart. | |
624 |
|
623 | |||
625 | Log files can later be reloaded with the -logplay option and IPython |
|
624 | Log files can later be reloaded with the -logplay option and IPython | |
626 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
625 | will attempt to 'replay' the log by executing all the lines in it, thus | |
627 | restoring the state of a previous session. This feature is not quite |
|
626 | restoring the state of a previous session. This feature is not quite | |
628 | perfect, but can still be useful in many cases. |
|
627 | perfect, but can still be useful in many cases. | |
629 |
|
628 | |||
630 | The log files can also be used as a way to have a permanent record of |
|
629 | The log files can also be used as a way to have a permanent record of | |
631 | any code you wrote while experimenting. Log files are regular text files |
|
630 | any code you wrote while experimenting. Log files are regular text files | |
632 | which you can later open in your favorite text editor to extract code or |
|
631 | which you can later open in your favorite text editor to extract code or | |
633 | to 'clean them up' before using them to replay a session. |
|
632 | to 'clean them up' before using them to replay a session. | |
634 |
|
633 | |||
635 | The %logstart function for activating logging in mid-session is used as |
|
634 | The %logstart function for activating logging in mid-session is used as | |
636 | follows: |
|
635 | follows: | |
637 |
|
636 | |||
638 | %logstart [log_name [log_mode]] |
|
637 | %logstart [log_name [log_mode]] | |
639 |
|
638 | |||
640 | If no name is given, it defaults to a file named 'log' in your |
|
639 | If no name is given, it defaults to a file named 'log' in your | |
641 | IPYTHON_DIR directory, in 'rotate' mode (see below). |
|
640 | IPYTHON_DIR directory, in 'rotate' mode (see below). | |
642 |
|
641 | |||
643 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
642 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |
644 | history up to that point and then continues logging. |
|
643 | history up to that point and then continues logging. | |
645 |
|
644 | |||
646 | %logstart takes a second optional parameter: logging mode. This can be |
|
645 | %logstart takes a second optional parameter: logging mode. This can be | |
647 | one of (note that the modes are given unquoted): |
|
646 | one of (note that the modes are given unquoted): | |
648 |
|
647 | |||
649 | * [over:] overwrite existing log_name. |
|
648 | * [over:] overwrite existing log_name. | |
650 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
649 | * [backup:] rename (if exists) to log_name~ and start log_name. | |
651 | * [append:] well, that says it. |
|
650 | * [append:] well, that says it. | |
652 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
651 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. | |
653 |
|
652 | |||
654 | The %logoff and %logon functions allow you to temporarily stop and |
|
653 | The %logoff and %logon functions allow you to temporarily stop and | |
655 | resume logging to a file which had previously been started with |
|
654 | resume logging to a file which had previously been started with | |
656 | %logstart. They will fail (with an explanation) if you try to use them |
|
655 | %logstart. They will fail (with an explanation) if you try to use them | |
657 | before logging has been started. |
|
656 | before logging has been started. | |
658 |
|
657 | |||
659 | .. _system_shell_access: |
|
658 | .. _system_shell_access: | |
660 |
|
659 | |||
661 | System shell access |
|
660 | System shell access | |
662 | ------------------- |
|
661 | ------------------- | |
663 |
|
662 | |||
664 | Any input line beginning with a ! character is passed verbatim (minus |
|
663 | Any input line beginning with a ! character is passed verbatim (minus | |
665 | the !, of course) to the underlying operating system. For example, |
|
664 | the !, of course) to the underlying operating system. For example, | |
666 | typing !ls will run 'ls' in the current directory. |
|
665 | typing !ls will run 'ls' in the current directory. | |
667 |
|
666 | |||
668 | Manual capture of command output |
|
667 | Manual capture of command output | |
669 | -------------------------------- |
|
668 | -------------------------------- | |
670 |
|
669 | |||
671 | If the input line begins with two exclamation marks, !!, the command is |
|
670 | If the input line begins with two exclamation marks, !!, the command is | |
672 | executed but its output is captured and returned as a python list, split |
|
671 | executed but its output is captured and returned as a python list, split | |
673 | on newlines. Any output sent by the subprocess to standard error is |
|
672 | on newlines. Any output sent by the subprocess to standard error is | |
674 | printed separately, so that the resulting list only captures standard |
|
673 | printed separately, so that the resulting list only captures standard | |
675 | output. The !! syntax is a shorthand for the %sx magic command. |
|
674 | output. The !! syntax is a shorthand for the %sx magic command. | |
676 |
|
675 | |||
677 | Finally, the %sc magic (short for 'shell capture') is similar to %sx, |
|
676 | Finally, the %sc magic (short for 'shell capture') is similar to %sx, | |
678 | but allowing more fine-grained control of the capture details, and |
|
677 | but allowing more fine-grained control of the capture details, and | |
679 | storing the result directly into a named variable. The direct use of |
|
678 | storing the result directly into a named variable. The direct use of | |
680 | %sc is now deprecated, and you should ise the ``var = !cmd`` syntax |
|
679 | %sc is now deprecated, and you should ise the ``var = !cmd`` syntax | |
681 | instead. |
|
680 | instead. | |
682 |
|
681 | |||
683 | IPython also allows you to expand the value of python variables when |
|
682 | IPython also allows you to expand the value of python variables when | |
684 | making system calls. Any python variable or expression which you prepend |
|
683 | making system calls. Any python variable or expression which you prepend | |
685 | with $ will get expanded before the system call is made:: |
|
684 | with $ will get expanded before the system call is made:: | |
686 |
|
685 | |||
687 | In [1]: pyvar='Hello world' |
|
686 | In [1]: pyvar='Hello world' | |
688 | In [2]: !echo "A python variable: $pyvar" |
|
687 | In [2]: !echo "A python variable: $pyvar" | |
689 | A python variable: Hello world |
|
688 | A python variable: Hello world | |
690 |
|
689 | |||
691 | If you want the shell to actually see a literal $, you need to type it |
|
690 | If you want the shell to actually see a literal $, you need to type it | |
692 | twice:: |
|
691 | twice:: | |
693 |
|
692 | |||
694 | In [3]: !echo "A system variable: $$HOME" |
|
693 | In [3]: !echo "A system variable: $$HOME" | |
695 | A system variable: /home/fperez |
|
694 | A system variable: /home/fperez | |
696 |
|
695 | |||
697 | You can pass arbitrary expressions, though you'll need to delimit them |
|
696 | You can pass arbitrary expressions, though you'll need to delimit them | |
698 | with {} if there is ambiguity as to the extent of the expression:: |
|
697 | with {} if there is ambiguity as to the extent of the expression:: | |
699 |
|
698 | |||
700 | In [5]: x=10 |
|
699 | In [5]: x=10 | |
701 | In [6]: y=20 |
|
700 | In [6]: y=20 | |
702 | In [13]: !echo $x+y |
|
701 | In [13]: !echo $x+y | |
703 | 10+y |
|
702 | 10+y | |
704 | In [7]: !echo ${x+y} |
|
703 | In [7]: !echo ${x+y} | |
705 | 30 |
|
704 | 30 | |
706 |
|
705 | |||
707 | Even object attributes can be expanded:: |
|
706 | Even object attributes can be expanded:: | |
708 |
|
707 | |||
709 | In [12]: !echo $sys.argv |
|
708 | In [12]: !echo $sys.argv | |
710 | [/home/fperez/usr/bin/ipython] |
|
709 | [/home/fperez/usr/bin/ipython] | |
711 |
|
710 | |||
712 |
|
711 | |||
713 | System command aliases |
|
712 | System command aliases | |
714 | ---------------------- |
|
713 | ---------------------- | |
715 |
|
714 | |||
716 | The %alias magic function and the alias option in the ipythonrc |
|
715 | The %alias magic function and the alias option in the ipythonrc | |
717 | configuration file allow you to define magic functions which are in fact |
|
716 | configuration file allow you to define magic functions which are in fact | |
718 | system shell commands. These aliases can have parameters. |
|
717 | system shell commands. These aliases can have parameters. | |
719 |
|
718 | |||
720 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
719 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' | |
721 |
|
720 | |||
722 | Then, typing '%alias_name params' will execute the system command 'cmd |
|
721 | Then, typing '%alias_name params' will execute the system command 'cmd | |
723 | params' (from your underlying operating system). |
|
722 | params' (from your underlying operating system). | |
724 |
|
723 | |||
725 | You can also define aliases with parameters using %s specifiers (one per |
|
724 | You can also define aliases with parameters using %s specifiers (one per | |
726 | parameter). The following example defines the %parts function as an |
|
725 | parameter). The following example defines the %parts function as an | |
727 | alias to the command 'echo first %s second %s' where each %s will be |
|
726 | alias to the command 'echo first %s second %s' where each %s will be | |
728 | replaced by a positional parameter to the call to %parts:: |
|
727 | replaced by a positional parameter to the call to %parts:: | |
729 |
|
728 | |||
730 | In [1]: alias parts echo first %s second %s |
|
729 | In [1]: alias parts echo first %s second %s | |
731 | In [2]: %parts A B |
|
730 | In [2]: %parts A B | |
732 | first A second B |
|
731 | first A second B | |
733 | In [3]: %parts A |
|
732 | In [3]: %parts A | |
734 | Incorrect number of arguments: 2 expected. |
|
733 | Incorrect number of arguments: 2 expected. | |
735 | parts is an alias to: 'echo first %s second %s' |
|
734 | parts is an alias to: 'echo first %s second %s' | |
736 |
|
735 | |||
737 | If called with no parameters, %alias prints the table of currently |
|
736 | If called with no parameters, %alias prints the table of currently | |
738 | defined aliases. |
|
737 | defined aliases. | |
739 |
|
738 | |||
740 | The %rehash/rehashx magics allow you to load your entire $PATH as |
|
739 | The %rehash/rehashx magics allow you to load your entire $PATH as | |
741 | ipython aliases. See their respective docstrings (or sec. 6.2 |
|
740 | ipython aliases. See their respective docstrings (or sec. 6.2 | |
742 | <#sec:magic> for further details). |
|
741 | <#sec:magic> for further details). | |
743 |
|
742 | |||
744 |
|
743 | |||
745 | .. _dreload: |
|
744 | .. _dreload: | |
746 |
|
745 | |||
747 | Recursive reload |
|
746 | Recursive reload | |
748 | ---------------- |
|
747 | ---------------- | |
749 |
|
748 | |||
750 | The dreload function does a recursive reload of a module: changes made |
|
749 | The dreload function does a recursive reload of a module: changes made | |
751 | to the module since you imported will actually be available without |
|
750 | to the module since you imported will actually be available without | |
752 | having to exit. |
|
751 | having to exit. | |
753 |
|
752 | |||
754 |
|
753 | |||
755 | Verbose and colored exception traceback printouts |
|
754 | Verbose and colored exception traceback printouts | |
756 | ------------------------------------------------- |
|
755 | ------------------------------------------------- | |
757 |
|
756 | |||
758 | IPython provides the option to see very detailed exception tracebacks, |
|
757 | IPython provides the option to see very detailed exception tracebacks, | |
759 | which can be especially useful when debugging large programs. You can |
|
758 | which can be especially useful when debugging large programs. You can | |
760 | run any Python file with the %run function to benefit from these |
|
759 | run any Python file with the %run function to benefit from these | |
761 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
760 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can | |
762 | be colored (if your terminal supports it) which makes them much easier |
|
761 | be colored (if your terminal supports it) which makes them much easier | |
763 | to parse visually. |
|
762 | to parse visually. | |
764 |
|
763 | |||
765 | See the magic xmode and colors functions for details (just type %magic). |
|
764 | See the magic xmode and colors functions for details (just type %magic). | |
766 |
|
765 | |||
767 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
766 | These features are basically a terminal version of Ka-Ping Yee's cgitb | |
768 | module, now part of the standard Python library. |
|
767 | module, now part of the standard Python library. | |
769 |
|
768 | |||
770 |
|
769 | |||
771 | .. _input_caching: |
|
770 | .. _input_caching: | |
772 |
|
771 | |||
773 | Input caching system |
|
772 | Input caching system | |
774 | -------------------- |
|
773 | -------------------- | |
775 |
|
774 | |||
776 | IPython offers numbered prompts (In/Out) with input and output caching |
|
775 | IPython offers numbered prompts (In/Out) with input and output caching | |
777 | (also referred to as 'input history'). All input is saved and can be |
|
776 | (also referred to as 'input history'). All input is saved and can be | |
778 | retrieved as variables (besides the usual arrow key recall), in |
|
777 | retrieved as variables (besides the usual arrow key recall), in | |
779 | addition to the %rep magic command that brings a history entry |
|
778 | addition to the %rep magic command that brings a history entry | |
780 | up for editing on the next command line. |
|
779 | up for editing on the next command line. | |
781 |
|
780 | |||
782 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
781 | The following GLOBAL variables always exist (so don't overwrite them!): | |
783 | _i: stores previous input. _ii: next previous. _iii: next-next previous. |
|
782 | _i: stores previous input. _ii: next previous. _iii: next-next previous. | |
784 | _ih : a list of all input _ih[n] is the input from line n and this list |
|
783 | _ih : a list of all input _ih[n] is the input from line n and this list | |
785 | is aliased to the global variable In. If you overwrite In with a |
|
784 | is aliased to the global variable In. If you overwrite In with a | |
786 | variable of your own, you can remake the assignment to the internal list |
|
785 | variable of your own, you can remake the assignment to the internal list | |
787 | with a simple 'In=_ih'. |
|
786 | with a simple 'In=_ih'. | |
788 |
|
787 | |||
789 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
788 | Additionally, global variables named _i<n> are dynamically created (<n> | |
790 | being the prompt counter), such that |
|
789 | being the prompt counter), such that | |
791 | _i<n> == _ih[<n>] == In[<n>]. |
|
790 | _i<n> == _ih[<n>] == In[<n>]. | |
792 |
|
791 | |||
793 | For example, what you typed at prompt 14 is available as _i14, _ih[14] |
|
792 | For example, what you typed at prompt 14 is available as _i14, _ih[14] | |
794 | and In[14]. |
|
793 | and In[14]. | |
795 |
|
794 | |||
796 | This allows you to easily cut and paste multi line interactive prompts |
|
795 | This allows you to easily cut and paste multi line interactive prompts | |
797 | by printing them out: they print like a clean string, without prompt |
|
796 | by printing them out: they print like a clean string, without prompt | |
798 | characters. You can also manipulate them like regular variables (they |
|
797 | characters. You can also manipulate them like regular variables (they | |
799 | are strings), modify or exec them (typing 'exec _i9' will re-execute the |
|
798 | are strings), modify or exec them (typing 'exec _i9' will re-execute the | |
800 | contents of input prompt 9, 'exec In[9:14]+In[18]' will re-execute lines |
|
799 | contents of input prompt 9, 'exec In[9:14]+In[18]' will re-execute lines | |
801 | 9 through 13 and line 18). |
|
800 | 9 through 13 and line 18). | |
802 |
|
801 | |||
803 | You can also re-execute multiple lines of input easily by using the |
|
802 | You can also re-execute multiple lines of input easily by using the | |
804 | magic %macro function (which automates the process and allows |
|
803 | magic %macro function (which automates the process and allows | |
805 | re-execution without having to type 'exec' every time). The macro system |
|
804 | re-execution without having to type 'exec' every time). The macro system | |
806 | also allows you to re-execute previous lines which include magic |
|
805 | also allows you to re-execute previous lines which include magic | |
807 | function calls (which require special processing). Type %macro? or see |
|
806 | function calls (which require special processing). Type %macro? or see | |
808 | sec. 6.2 <#sec:magic> for more details on the macro system. |
|
807 | sec. 6.2 <#sec:magic> for more details on the macro system. | |
809 |
|
808 | |||
810 | A history function %hist allows you to see any part of your input |
|
809 | A history function %hist allows you to see any part of your input | |
811 | history by printing a range of the _i variables. |
|
810 | history by printing a range of the _i variables. | |
812 |
|
811 | |||
813 | You can also search ('grep') through your history by typing |
|
812 | You can also search ('grep') through your history by typing | |
814 | '%hist -g somestring'. This also searches through the so called *shadow history*, |
|
813 | '%hist -g somestring'. This also searches through the so called *shadow history*, | |
815 | which remembers all the commands (apart from multiline code blocks) |
|
814 | which remembers all the commands (apart from multiline code blocks) | |
816 | you have ever entered. Handy for searching for svn/bzr URL's, IP adrresses |
|
815 | you have ever entered. Handy for searching for svn/bzr URL's, IP adrresses | |
817 | etc. You can bring shadow history entries listed by '%hist -g' up for editing |
|
816 | etc. You can bring shadow history entries listed by '%hist -g' up for editing | |
818 | (or re-execution by just pressing ENTER) with %rep command. Shadow history |
|
817 | (or re-execution by just pressing ENTER) with %rep command. Shadow history | |
819 | entries are not available as _iNUMBER variables, and they are identified by |
|
818 | entries are not available as _iNUMBER variables, and they are identified by | |
820 | the '0' prefix in %hist -g output. That is, history entry 12 is a normal |
|
819 | the '0' prefix in %hist -g output. That is, history entry 12 is a normal | |
821 | history entry, but 0231 is a shadow history entry. |
|
820 | history entry, but 0231 is a shadow history entry. | |
822 |
|
821 | |||
823 | Shadow history was added because the readline history is inherently very |
|
822 | Shadow history was added because the readline history is inherently very | |
824 | unsafe - if you have multiple IPython sessions open, the last session |
|
823 | unsafe - if you have multiple IPython sessions open, the last session | |
825 | to close will overwrite the history of previountly closed session. Likewise, |
|
824 | to close will overwrite the history of previountly closed session. Likewise, | |
826 | if a crash occurs, history is never saved, whereas shadow history entries |
|
825 | if a crash occurs, history is never saved, whereas shadow history entries | |
827 | are added after entering every command (so a command executed |
|
826 | are added after entering every command (so a command executed | |
828 | in another IPython session is immediately available in other IPython |
|
827 | in another IPython session is immediately available in other IPython | |
829 | sessions that are open). |
|
828 | sessions that are open). | |
830 |
|
829 | |||
831 | To conserve space, a command can exist in shadow history only once - it doesn't |
|
830 | To conserve space, a command can exist in shadow history only once - it doesn't | |
832 | make sense to store a common line like "cd .." a thousand times. The idea is |
|
831 | make sense to store a common line like "cd .." a thousand times. The idea is | |
833 | mainly to provide a reliable place where valuable, hard-to-remember commands can |
|
832 | mainly to provide a reliable place where valuable, hard-to-remember commands can | |
834 | always be retrieved, as opposed to providing an exact sequence of commands |
|
833 | always be retrieved, as opposed to providing an exact sequence of commands | |
835 | you have entered in actual order. |
|
834 | you have entered in actual order. | |
836 |
|
835 | |||
837 | Because shadow history has all the commands you have ever executed, |
|
836 | Because shadow history has all the commands you have ever executed, | |
838 | time taken by %hist -g will increase oven time. If it ever starts to take |
|
837 | time taken by %hist -g will increase oven time. If it ever starts to take | |
839 | too long (or it ends up containing sensitive information like passwords), |
|
838 | too long (or it ends up containing sensitive information like passwords), | |
840 | clear the shadow history by `%clear shadow_nuke`. |
|
839 | clear the shadow history by `%clear shadow_nuke`. | |
841 |
|
840 | |||
842 | Time taken to add entries to shadow history should be negligible, but |
|
841 | Time taken to add entries to shadow history should be negligible, but | |
843 | in any case, if you start noticing performance degradation after using |
|
842 | in any case, if you start noticing performance degradation after using | |
844 | IPython for a long time (or running a script that floods the shadow history!), |
|
843 | IPython for a long time (or running a script that floods the shadow history!), | |
845 | you can 'compress' the shadow history by executing |
|
844 | you can 'compress' the shadow history by executing | |
846 | `%clear shadow_compress`. In practice, this should never be necessary |
|
845 | `%clear shadow_compress`. In practice, this should never be necessary | |
847 | in normal use. |
|
846 | in normal use. | |
848 |
|
847 | |||
849 | .. _output_caching: |
|
848 | .. _output_caching: | |
850 |
|
849 | |||
851 | Output caching system |
|
850 | Output caching system | |
852 | --------------------- |
|
851 | --------------------- | |
853 |
|
852 | |||
854 | For output that is returned from actions, a system similar to the input |
|
853 | For output that is returned from actions, a system similar to the input | |
855 | cache exists but using _ instead of _i. Only actions that produce a |
|
854 | cache exists but using _ instead of _i. Only actions that produce a | |
856 | result (NOT assignments, for example) are cached. If you are familiar |
|
855 | result (NOT assignments, for example) are cached. If you are familiar | |
857 | with Mathematica, IPython's _ variables behave exactly like |
|
856 | with Mathematica, IPython's _ variables behave exactly like | |
858 | Mathematica's % variables. |
|
857 | Mathematica's % variables. | |
859 |
|
858 | |||
860 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
859 | The following GLOBAL variables always exist (so don't overwrite them!): | |
861 |
|
860 | |||
862 | * [_] (a single underscore) : stores previous output, like Python's |
|
861 | * [_] (a single underscore) : stores previous output, like Python's | |
863 | default interpreter. |
|
862 | default interpreter. | |
864 | * [__] (two underscores): next previous. |
|
863 | * [__] (two underscores): next previous. | |
865 | * [___] (three underscores): next-next previous. |
|
864 | * [___] (three underscores): next-next previous. | |
866 |
|
865 | |||
867 | Additionally, global variables named _<n> are dynamically created (<n> |
|
866 | Additionally, global variables named _<n> are dynamically created (<n> | |
868 | being the prompt counter), such that the result of output <n> is always |
|
867 | being the prompt counter), such that the result of output <n> is always | |
869 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
868 | available as _<n> (don't use the angle brackets, just the number, e.g. | |
870 | _21). |
|
869 | _21). | |
871 |
|
870 | |||
872 | These global variables are all stored in a global dictionary (not a |
|
871 | These global variables are all stored in a global dictionary (not a | |
873 | list, since it only has entries for lines which returned a result) |
|
872 | list, since it only has entries for lines which returned a result) | |
874 | available under the names _oh and Out (similar to _ih and In). So the |
|
873 | available under the names _oh and Out (similar to _ih and In). So the | |
875 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you |
|
874 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you | |
876 | accidentally overwrite the Out variable you can recover it by typing |
|
875 | accidentally overwrite the Out variable you can recover it by typing | |
877 | 'Out=_oh' at the prompt. |
|
876 | 'Out=_oh' at the prompt. | |
878 |
|
877 | |||
879 | This system obviously can potentially put heavy memory demands on your |
|
878 | This system obviously can potentially put heavy memory demands on your | |
880 | system, since it prevents Python's garbage collector from removing any |
|
879 | system, since it prevents Python's garbage collector from removing any | |
881 | previously computed results. You can control how many results are kept |
|
880 | previously computed results. You can control how many results are kept | |
882 | in memory with the option (at the command line or in your ipythonrc |
|
881 | in memory with the option (at the command line or in your ipythonrc | |
883 | file) cache_size. If you set it to 0, the whole system is completely |
|
882 | file) cache_size. If you set it to 0, the whole system is completely | |
884 | disabled and the prompts revert to the classic '>>>' of normal Python. |
|
883 | disabled and the prompts revert to the classic '>>>' of normal Python. | |
885 |
|
884 | |||
886 |
|
885 | |||
887 | Directory history |
|
886 | Directory history | |
888 | ----------------- |
|
887 | ----------------- | |
889 |
|
888 | |||
890 | Your history of visited directories is kept in the global list _dh, and |
|
889 | Your history of visited directories is kept in the global list _dh, and | |
891 | the magic %cd command can be used to go to any entry in that list. The |
|
890 | the magic %cd command can be used to go to any entry in that list. The | |
892 | %dhist command allows you to view this history. Do ``cd -<TAB`` to |
|
891 | %dhist command allows you to view this history. Do ``cd -<TAB`` to | |
893 | conventiently view the directory history. |
|
892 | conventiently view the directory history. | |
894 |
|
893 | |||
895 |
|
894 | |||
896 | Automatic parentheses and quotes |
|
895 | Automatic parentheses and quotes | |
897 | -------------------------------- |
|
896 | -------------------------------- | |
898 |
|
897 | |||
899 | These features were adapted from Nathan Gray's LazyPython. They are |
|
898 | These features were adapted from Nathan Gray's LazyPython. They are | |
900 | meant to allow less typing for common situations. |
|
899 | meant to allow less typing for common situations. | |
901 |
|
900 | |||
902 |
|
901 | |||
903 | Automatic parentheses |
|
902 | Automatic parentheses | |
904 | --------------------- |
|
903 | --------------------- | |
905 |
|
904 | |||
906 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
905 | Callable objects (i.e. functions, methods, etc) can be invoked like this | |
907 | (notice the commas between the arguments):: |
|
906 | (notice the commas between the arguments):: | |
908 |
|
907 | |||
909 | >>> callable_ob arg1, arg2, arg3 |
|
908 | >>> callable_ob arg1, arg2, arg3 | |
910 |
|
909 | |||
911 | and the input will be translated to this:: |
|
910 | and the input will be translated to this:: | |
912 |
|
911 | |||
913 | -> callable_ob(arg1, arg2, arg3) |
|
912 | -> callable_ob(arg1, arg2, arg3) | |
914 |
|
913 | |||
915 | You can force automatic parentheses by using '/' as the first character |
|
914 | You can force automatic parentheses by using '/' as the first character | |
916 | of a line. For example:: |
|
915 | of a line. For example:: | |
917 |
|
916 | |||
918 | >>> /globals # becomes 'globals()' |
|
917 | >>> /globals # becomes 'globals()' | |
919 |
|
918 | |||
920 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
919 | Note that the '/' MUST be the first character on the line! This won't work:: | |
921 |
|
920 | |||
922 | >>> print /globals # syntax error |
|
921 | >>> print /globals # syntax error | |
923 |
|
922 | |||
924 | In most cases the automatic algorithm should work, so you should rarely |
|
923 | In most cases the automatic algorithm should work, so you should rarely | |
925 | need to explicitly invoke /. One notable exception is if you are trying |
|
924 | need to explicitly invoke /. One notable exception is if you are trying | |
926 | to call a function with a list of tuples as arguments (the parenthesis |
|
925 | to call a function with a list of tuples as arguments (the parenthesis | |
927 | will confuse IPython):: |
|
926 | will confuse IPython):: | |
928 |
|
927 | |||
929 | In [1]: zip (1,2,3),(4,5,6) # won't work |
|
928 | In [1]: zip (1,2,3),(4,5,6) # won't work | |
930 |
|
929 | |||
931 | but this will work:: |
|
930 | but this will work:: | |
932 |
|
931 | |||
933 | In [2]: /zip (1,2,3),(4,5,6) |
|
932 | In [2]: /zip (1,2,3),(4,5,6) | |
934 | ---> zip ((1,2,3),(4,5,6)) |
|
933 | ---> zip ((1,2,3),(4,5,6)) | |
935 | Out[2]= [(1, 4), (2, 5), (3, 6)] |
|
934 | Out[2]= [(1, 4), (2, 5), (3, 6)] | |
936 |
|
935 | |||
937 | IPython tells you that it has altered your command line by displaying |
|
936 | IPython tells you that it has altered your command line by displaying | |
938 | the new command line preceded by ->. e.g.:: |
|
937 | the new command line preceded by ->. e.g.:: | |
939 |
|
938 | |||
940 | In [18]: callable list |
|
939 | In [18]: callable list | |
941 | ----> callable (list) |
|
940 | ----> callable (list) | |
942 |
|
941 | |||
943 |
|
942 | |||
944 | Automatic quoting |
|
943 | Automatic quoting | |
945 | ----------------- |
|
944 | ----------------- | |
946 |
|
945 | |||
947 | You can force automatic quoting of a function's arguments by using ',' |
|
946 | You can force automatic quoting of a function's arguments by using ',' | |
948 | or ';' as the first character of a line. For example:: |
|
947 | or ';' as the first character of a line. For example:: | |
949 |
|
948 | |||
950 | >>> ,my_function /home/me # becomes my_function("/home/me") |
|
949 | >>> ,my_function /home/me # becomes my_function("/home/me") | |
951 |
|
950 | |||
952 | If you use ';' instead, the whole argument is quoted as a single string |
|
951 | If you use ';' instead, the whole argument is quoted as a single string | |
953 | (while ',' splits on whitespace):: |
|
952 | (while ',' splits on whitespace):: | |
954 |
|
953 | |||
955 | >>> ,my_function a b c # becomes my_function("a","b","c") |
|
954 | >>> ,my_function a b c # becomes my_function("a","b","c") | |
956 |
|
955 | |||
957 | >>> ;my_function a b c # becomes my_function("a b c") |
|
956 | >>> ;my_function a b c # becomes my_function("a b c") | |
958 |
|
957 | |||
959 | Note that the ',' or ';' MUST be the first character on the line! This |
|
958 | Note that the ',' or ';' MUST be the first character on the line! This | |
960 | won't work:: |
|
959 | won't work:: | |
961 |
|
960 | |||
962 | >>> x = ,my_function /home/me # syntax error |
|
961 | >>> x = ,my_function /home/me # syntax error | |
963 |
|
962 | |||
964 | IPython as your default Python environment |
|
963 | IPython as your default Python environment | |
965 | ========================================== |
|
964 | ========================================== | |
966 |
|
965 | |||
967 | Python honors the environment variable PYTHONSTARTUP and will execute at |
|
966 | Python honors the environment variable PYTHONSTARTUP and will execute at | |
968 | startup the file referenced by this variable. If you put at the end of |
|
967 | startup the file referenced by this variable. If you put at the end of | |
969 | this file the following two lines of code:: |
|
968 | this file the following two lines of code:: | |
970 |
|
969 | |||
971 | import IPython |
|
970 | import IPython | |
972 | IPython.Shell.IPShell().mainloop(sys_exit=1) |
|
971 | IPython.Shell.IPShell().mainloop(sys_exit=1) | |
973 |
|
972 | |||
974 | then IPython will be your working environment anytime you start Python. |
|
973 | then IPython will be your working environment anytime you start Python. | |
975 | The sys_exit=1 is needed to have IPython issue a call to sys.exit() when |
|
974 | The sys_exit=1 is needed to have IPython issue a call to sys.exit() when | |
976 | it finishes, otherwise you'll be back at the normal Python '>>>' |
|
975 | it finishes, otherwise you'll be back at the normal Python '>>>' | |
977 | prompt. |
|
976 | prompt. | |
978 |
|
977 | |||
979 | This is probably useful to developers who manage multiple Python |
|
978 | This is probably useful to developers who manage multiple Python | |
980 | versions and don't want to have correspondingly multiple IPython |
|
979 | versions and don't want to have correspondingly multiple IPython | |
981 | versions. Note that in this mode, there is no way to pass IPython any |
|
980 | versions. Note that in this mode, there is no way to pass IPython any | |
982 | command-line options, as those are trapped first by Python itself. |
|
981 | command-line options, as those are trapped first by Python itself. | |
983 |
|
982 | |||
984 | .. _Embedding: |
|
983 | .. _Embedding: | |
985 |
|
984 | |||
986 | Embedding IPython |
|
985 | Embedding IPython | |
987 | ================= |
|
986 | ================= | |
988 |
|
987 | |||
989 | It is possible to start an IPython instance inside your own Python |
|
988 | It is possible to start an IPython instance inside your own Python | |
990 | programs. This allows you to evaluate dynamically the state of your |
|
989 | programs. This allows you to evaluate dynamically the state of your | |
991 | code, operate with your variables, analyze them, etc. Note however that |
|
990 | code, operate with your variables, analyze them, etc. Note however that | |
992 | any changes you make to values while in the shell do not propagate back |
|
991 | any changes you make to values while in the shell do not propagate back | |
993 | to the running code, so it is safe to modify your values because you |
|
992 | to the running code, so it is safe to modify your values because you | |
994 | won't break your code in bizarre ways by doing so. |
|
993 | won't break your code in bizarre ways by doing so. | |
995 |
|
994 | |||
996 | This feature allows you to easily have a fully functional python |
|
995 | This feature allows you to easily have a fully functional python | |
997 | environment for doing object introspection anywhere in your code with a |
|
996 | environment for doing object introspection anywhere in your code with a | |
998 | simple function call. In some cases a simple print statement is enough, |
|
997 | simple function call. In some cases a simple print statement is enough, | |
999 | but if you need to do more detailed analysis of a code fragment this |
|
998 | but if you need to do more detailed analysis of a code fragment this | |
1000 | feature can be very valuable. |
|
999 | feature can be very valuable. | |
1001 |
|
1000 | |||
1002 | It can also be useful in scientific computing situations where it is |
|
1001 | It can also be useful in scientific computing situations where it is | |
1003 | common to need to do some automatic, computationally intensive part and |
|
1002 | common to need to do some automatic, computationally intensive part and | |
1004 | then stop to look at data, plots, etc. |
|
1003 | then stop to look at data, plots, etc. | |
1005 | Opening an IPython instance will give you full access to your data and |
|
1004 | Opening an IPython instance will give you full access to your data and | |
1006 | functions, and you can resume program execution once you are done with |
|
1005 | functions, and you can resume program execution once you are done with | |
1007 | the interactive part (perhaps to stop again later, as many times as |
|
1006 | the interactive part (perhaps to stop again later, as many times as | |
1008 | needed). |
|
1007 | needed). | |
1009 |
|
1008 | |||
1010 | The following code snippet is the bare minimum you need to include in |
|
1009 | The following code snippet is the bare minimum you need to include in | |
1011 | your Python programs for this to work (detailed examples follow later):: |
|
1010 | your Python programs for this to work (detailed examples follow later):: | |
1012 |
|
1011 | |||
1013 | from IPython.Shell import IPShellEmbed |
|
1012 | from IPython.Shell import IPShellEmbed | |
1014 |
|
1013 | |||
1015 | ipshell = IPShellEmbed() |
|
1014 | ipshell = IPShellEmbed() | |
1016 |
|
1015 | |||
1017 | ipshell() # this call anywhere in your program will start IPython |
|
1016 | ipshell() # this call anywhere in your program will start IPython | |
1018 |
|
1017 | |||
1019 | You can run embedded instances even in code which is itself being run at |
|
1018 | You can run embedded instances even in code which is itself being run at | |
1020 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
1019 | the IPython interactive prompt with '%run <filename>'. Since it's easy | |
1021 | to get lost as to where you are (in your top-level IPython or in your |
|
1020 | to get lost as to where you are (in your top-level IPython or in your | |
1022 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
1021 | embedded one), it's a good idea in such cases to set the in/out prompts | |
1023 | to something different for the embedded instances. The code examples |
|
1022 | to something different for the embedded instances. The code examples | |
1024 | below illustrate this. |
|
1023 | below illustrate this. | |
1025 |
|
1024 | |||
1026 | You can also have multiple IPython instances in your program and open |
|
1025 | You can also have multiple IPython instances in your program and open | |
1027 | them separately, for example with different options for data |
|
1026 | them separately, for example with different options for data | |
1028 | presentation. If you close and open the same instance multiple times, |
|
1027 | presentation. If you close and open the same instance multiple times, | |
1029 | its prompt counters simply continue from each execution to the next. |
|
1028 | its prompt counters simply continue from each execution to the next. | |
1030 |
|
1029 | |||
1031 | Please look at the docstrings in the Shell.py module for more details on |
|
1030 | Please look at the docstrings in the Shell.py module for more details on | |
1032 | the use of this system. |
|
1031 | the use of this system. | |
1033 |
|
1032 | |||
1034 | The following sample file illustrating how to use the embedding |
|
1033 | The following sample file illustrating how to use the embedding | |
1035 | functionality is provided in the examples directory as example-embed.py. |
|
1034 | functionality is provided in the examples directory as example-embed.py. | |
1036 | It should be fairly self-explanatory:: |
|
1035 | It should be fairly self-explanatory:: | |
1037 |
|
1036 | |||
1038 |
|
1037 | |||
1039 | #!/usr/bin/env python |
|
1038 | #!/usr/bin/env python | |
1040 |
|
1039 | |||
1041 | """An example of how to embed an IPython shell into a running program. |
|
1040 | """An example of how to embed an IPython shell into a running program. | |
1042 |
|
1041 | |||
1043 | Please see the documentation in the IPython.Shell module for more details. |
|
1042 | Please see the documentation in the IPython.Shell module for more details. | |
1044 |
|
1043 | |||
1045 | The accompanying file example-embed-short.py has quick code fragments for |
|
1044 | The accompanying file example-embed-short.py has quick code fragments for | |
1046 | embedding which you can cut and paste in your code once you understand how |
|
1045 | embedding which you can cut and paste in your code once you understand how | |
1047 | things work. |
|
1046 | things work. | |
1048 |
|
1047 | |||
1049 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
1048 | The code in this file is deliberately extra-verbose, meant for learning.""" | |
1050 |
|
1049 | |||
1051 | # The basics to get you going: |
|
1050 | # The basics to get you going: | |
1052 |
|
1051 | |||
1053 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
1052 | # IPython sets the __IPYTHON__ variable so you can know if you have nested | |
1054 | # copies running. |
|
1053 | # copies running. | |
1055 |
|
1054 | |||
1056 | # Try running this code both at the command line and from inside IPython (with |
|
1055 | # Try running this code both at the command line and from inside IPython (with | |
1057 | # %run example-embed.py) |
|
1056 | # %run example-embed.py) | |
1058 | try: |
|
1057 | try: | |
1059 | __IPYTHON__ |
|
1058 | __IPYTHON__ | |
1060 | except NameError: |
|
1059 | except NameError: | |
1061 | nested = 0 |
|
1060 | nested = 0 | |
1062 | args = [''] |
|
1061 | args = [''] | |
1063 | else: |
|
1062 | else: | |
1064 | print "Running nested copies of IPython." |
|
1063 | print "Running nested copies of IPython." | |
1065 | print "The prompts for the nested copy have been modified" |
|
1064 | print "The prompts for the nested copy have been modified" | |
1066 | nested = 1 |
|
1065 | nested = 1 | |
1067 | # what the embedded instance will see as sys.argv: |
|
1066 | # what the embedded instance will see as sys.argv: | |
1068 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', |
|
1067 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
1069 | '-po','Out<\\#>: ','-nosep'] |
|
1068 | '-po','Out<\\#>: ','-nosep'] | |
1070 |
|
1069 | |||
1071 | # First import the embeddable shell class |
|
1070 | # First import the embeddable shell class | |
1072 | from IPython.Shell import IPShellEmbed |
|
1071 | from IPython.Shell import IPShellEmbed | |
1073 |
|
1072 | |||
1074 | # Now create an instance of the embeddable shell. The first argument is a |
|
1073 | # Now create an instance of the embeddable shell. The first argument is a | |
1075 | # string with options exactly as you would type them if you were starting |
|
1074 | # string with options exactly as you would type them if you were starting | |
1076 | # IPython at the system command line. Any parameters you want to define for |
|
1075 | # IPython at the system command line. Any parameters you want to define for | |
1077 | # configuration can thus be specified here. |
|
1076 | # configuration can thus be specified here. | |
1078 | ipshell = IPShellEmbed(args, |
|
1077 | ipshell = IPShellEmbed(args, | |
1079 | banner = 'Dropping into IPython', |
|
1078 | banner = 'Dropping into IPython', | |
1080 | exit_msg = 'Leaving Interpreter, back to program.') |
|
1079 | exit_msg = 'Leaving Interpreter, back to program.') | |
1081 |
|
1080 | |||
1082 | # Make a second instance, you can have as many as you want. |
|
1081 | # Make a second instance, you can have as many as you want. | |
1083 | if nested: |
|
1082 | if nested: | |
1084 | args[1] = 'In2<\\#>' |
|
1083 | args[1] = 'In2<\\#>' | |
1085 | else: |
|
1084 | else: | |
1086 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', |
|
1085 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
1087 | '-po','Out<\\#>: ','-nosep'] |
|
1086 | '-po','Out<\\#>: ','-nosep'] | |
1088 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
1087 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
1089 |
|
1088 | |||
1090 | print '\nHello. This is printed from the main controller program.\n' |
|
1089 | print '\nHello. This is printed from the main controller program.\n' | |
1091 |
|
1090 | |||
1092 | # You can then call ipshell() anywhere you need it (with an optional |
|
1091 | # You can then call ipshell() anywhere you need it (with an optional | |
1093 | # message): |
|
1092 | # message): | |
1094 | ipshell('***Called from top level. ' |
|
1093 | ipshell('***Called from top level. ' | |
1095 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
1094 | 'Hit Ctrl-D to exit interpreter and continue program.\n' | |
1096 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
1095 | 'Note that if you use %kill_embedded, you can fully deactivate\n' | |
1097 | 'This embedded instance so it will never turn on again') |
|
1096 | 'This embedded instance so it will never turn on again') | |
1098 |
|
1097 | |||
1099 | print '\nBack in caller program, moving along...\n' |
|
1098 | print '\nBack in caller program, moving along...\n' | |
1100 |
|
1099 | |||
1101 | #--------------------------------------------------------------------------- |
|
1100 | #--------------------------------------------------------------------------- | |
1102 | # More details: |
|
1101 | # More details: | |
1103 |
|
1102 | |||
1104 | # IPShellEmbed instances don't print the standard system banner and |
|
1103 | # IPShellEmbed instances don't print the standard system banner and | |
1105 | # messages. The IPython banner (which actually may contain initialization |
|
1104 | # messages. The IPython banner (which actually may contain initialization | |
1106 | # messages) is available as <instance>.IP.BANNER in case you want it. |
|
1105 | # messages) is available as <instance>.IP.BANNER in case you want it. | |
1107 |
|
1106 | |||
1108 | # IPShellEmbed instances print the following information everytime they |
|
1107 | # IPShellEmbed instances print the following information everytime they | |
1109 | # start: |
|
1108 | # start: | |
1110 |
|
1109 | |||
1111 | # - A global startup banner. |
|
1110 | # - A global startup banner. | |
1112 |
|
1111 | |||
1113 | # - A call-specific header string, which you can use to indicate where in the |
|
1112 | # - A call-specific header string, which you can use to indicate where in the | |
1114 | # execution flow the shell is starting. |
|
1113 | # execution flow the shell is starting. | |
1115 |
|
1114 | |||
1116 | # They also print an exit message every time they exit. |
|
1115 | # They also print an exit message every time they exit. | |
1117 |
|
1116 | |||
1118 | # Both the startup banner and the exit message default to None, and can be set |
|
1117 | # Both the startup banner and the exit message default to None, and can be set | |
1119 | # either at the instance constructor or at any other time with the |
|
1118 | # either at the instance constructor or at any other time with the | |
1120 | # set_banner() and set_exit_msg() methods. |
|
1119 | # set_banner() and set_exit_msg() methods. | |
1121 |
|
1120 | |||
1122 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
1121 | # The shell instance can be also put in 'dummy' mode globally or on a per-call | |
1123 | # basis. This gives you fine control for debugging without having to change |
|
1122 | # basis. This gives you fine control for debugging without having to change | |
1124 | # code all over the place. |
|
1123 | # code all over the place. | |
1125 |
|
1124 | |||
1126 | # The code below illustrates all this. |
|
1125 | # The code below illustrates all this. | |
1127 |
|
1126 | |||
1128 |
|
1127 | |||
1129 | # This is how the global banner and exit_msg can be reset at any point |
|
1128 | # This is how the global banner and exit_msg can be reset at any point | |
1130 | ipshell.set_banner('Entering interpreter - New Banner') |
|
1129 | ipshell.set_banner('Entering interpreter - New Banner') | |
1131 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') |
|
1130 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') | |
1132 |
|
1131 | |||
1133 | def foo(m): |
|
1132 | def foo(m): | |
1134 | s = 'spam' |
|
1133 | s = 'spam' | |
1135 | ipshell('***In foo(). Try @whos, or print s or m:') |
|
1134 | ipshell('***In foo(). Try @whos, or print s or m:') | |
1136 | print 'foo says m = ',m |
|
1135 | print 'foo says m = ',m | |
1137 |
|
1136 | |||
1138 | def bar(n): |
|
1137 | def bar(n): | |
1139 | s = 'eggs' |
|
1138 | s = 'eggs' | |
1140 | ipshell('***In bar(). Try @whos, or print s or n:') |
|
1139 | ipshell('***In bar(). Try @whos, or print s or n:') | |
1141 | print 'bar says n = ',n |
|
1140 | print 'bar says n = ',n | |
1142 |
|
1141 | |||
1143 | # Some calls to the above functions which will trigger IPython: |
|
1142 | # Some calls to the above functions which will trigger IPython: | |
1144 | print 'Main program calling foo("eggs")\n' |
|
1143 | print 'Main program calling foo("eggs")\n' | |
1145 | foo('eggs') |
|
1144 | foo('eggs') | |
1146 |
|
1145 | |||
1147 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
1146 | # The shell can be put in 'dummy' mode where calls to it silently return. This | |
1148 | # allows you, for example, to globally turn off debugging for a program with a |
|
1147 | # allows you, for example, to globally turn off debugging for a program with a | |
1149 | # single call. |
|
1148 | # single call. | |
1150 | ipshell.set_dummy_mode(1) |
|
1149 | ipshell.set_dummy_mode(1) | |
1151 | print '\nTrying to call IPython which is now "dummy":' |
|
1150 | print '\nTrying to call IPython which is now "dummy":' | |
1152 | ipshell() |
|
1151 | ipshell() | |
1153 | print 'Nothing happened...' |
|
1152 | print 'Nothing happened...' | |
1154 | # The global 'dummy' mode can still be overridden for a single call |
|
1153 | # The global 'dummy' mode can still be overridden for a single call | |
1155 | print '\nOverriding dummy mode manually:' |
|
1154 | print '\nOverriding dummy mode manually:' | |
1156 | ipshell(dummy=0) |
|
1155 | ipshell(dummy=0) | |
1157 |
|
1156 | |||
1158 | # Reactivate the IPython shell |
|
1157 | # Reactivate the IPython shell | |
1159 | ipshell.set_dummy_mode(0) |
|
1158 | ipshell.set_dummy_mode(0) | |
1160 |
|
1159 | |||
1161 | print 'You can even have multiple embedded instances:' |
|
1160 | print 'You can even have multiple embedded instances:' | |
1162 | ipshell2() |
|
1161 | ipshell2() | |
1163 |
|
1162 | |||
1164 | print '\nMain program calling bar("spam")\n' |
|
1163 | print '\nMain program calling bar("spam")\n' | |
1165 | bar('spam') |
|
1164 | bar('spam') | |
1166 |
|
1165 | |||
1167 | print 'Main program finished. Bye!' |
|
1166 | print 'Main program finished. Bye!' | |
1168 |
|
1167 | |||
1169 | #********************** End of file <example-embed.py> *********************** |
|
1168 | #********************** End of file <example-embed.py> *********************** | |
1170 |
|
1169 | |||
1171 | Once you understand how the system functions, you can use the following |
|
1170 | Once you understand how the system functions, you can use the following | |
1172 | code fragments in your programs which are ready for cut and paste:: |
|
1171 | code fragments in your programs which are ready for cut and paste:: | |
1173 |
|
1172 | |||
1174 |
|
1173 | |||
1175 | """Quick code snippets for embedding IPython into other programs. |
|
1174 | """Quick code snippets for embedding IPython into other programs. | |
1176 |
|
1175 | |||
1177 | See example-embed.py for full details, this file has the bare minimum code for |
|
1176 | See example-embed.py for full details, this file has the bare minimum code for | |
1178 | cut and paste use once you understand how to use the system.""" |
|
1177 | cut and paste use once you understand how to use the system.""" | |
1179 |
|
1178 | |||
1180 | #--------------------------------------------------------------------------- |
|
1179 | #--------------------------------------------------------------------------- | |
1181 | # This code loads IPython but modifies a few things if it detects it's running |
|
1180 | # This code loads IPython but modifies a few things if it detects it's running | |
1182 | # embedded in another IPython session (helps avoid confusion) |
|
1181 | # embedded in another IPython session (helps avoid confusion) | |
1183 |
|
1182 | |||
1184 | try: |
|
1183 | try: | |
1185 | __IPYTHON__ |
|
1184 | __IPYTHON__ | |
1186 | except NameError: |
|
1185 | except NameError: | |
1187 | argv = [''] |
|
1186 | argv = [''] | |
1188 | banner = exit_msg = '' |
|
1187 | banner = exit_msg = '' | |
1189 | else: |
|
1188 | else: | |
1190 | # Command-line options for IPython (a list like sys.argv) |
|
1189 | # Command-line options for IPython (a list like sys.argv) | |
1191 | argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:'] |
|
1190 | argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:'] | |
1192 | banner = '*** Nested interpreter ***' |
|
1191 | banner = '*** Nested interpreter ***' | |
1193 | exit_msg = '*** Back in main IPython ***' |
|
1192 | exit_msg = '*** Back in main IPython ***' | |
1194 |
|
1193 | |||
1195 | # First import the embeddable shell class |
|
1194 | # First import the embeddable shell class | |
1196 | from IPython.Shell import IPShellEmbed |
|
1195 | from IPython.Shell import IPShellEmbed | |
1197 | # Now create the IPython shell instance. Put ipshell() anywhere in your code |
|
1196 | # Now create the IPython shell instance. Put ipshell() anywhere in your code | |
1198 | # where you want it to open. |
|
1197 | # where you want it to open. | |
1199 | ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg) |
|
1198 | ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg) | |
1200 |
|
1199 | |||
1201 | #--------------------------------------------------------------------------- |
|
1200 | #--------------------------------------------------------------------------- | |
1202 | # This code will load an embeddable IPython shell always with no changes for |
|
1201 | # This code will load an embeddable IPython shell always with no changes for | |
1203 | # nested embededings. |
|
1202 | # nested embededings. | |
1204 |
|
1203 | |||
1205 | from IPython.Shell import IPShellEmbed |
|
1204 | from IPython.Shell import IPShellEmbed | |
1206 | ipshell = IPShellEmbed() |
|
1205 | ipshell = IPShellEmbed() | |
1207 | # Now ipshell() will open IPython anywhere in the code. |
|
1206 | # Now ipshell() will open IPython anywhere in the code. | |
1208 |
|
1207 | |||
1209 | #--------------------------------------------------------------------------- |
|
1208 | #--------------------------------------------------------------------------- | |
1210 | # This code loads an embeddable shell only if NOT running inside |
|
1209 | # This code loads an embeddable shell only if NOT running inside | |
1211 | # IPython. Inside IPython, the embeddable shell variable ipshell is just a |
|
1210 | # IPython. Inside IPython, the embeddable shell variable ipshell is just a | |
1212 | # dummy function. |
|
1211 | # dummy function. | |
1213 |
|
1212 | |||
1214 | try: |
|
1213 | try: | |
1215 | __IPYTHON__ |
|
1214 | __IPYTHON__ | |
1216 | except NameError: |
|
1215 | except NameError: | |
1217 | from IPython.Shell import IPShellEmbed |
|
1216 | from IPython.Shell import IPShellEmbed | |
1218 | ipshell = IPShellEmbed() |
|
1217 | ipshell = IPShellEmbed() | |
1219 | # Now ipshell() will open IPython anywhere in the code |
|
1218 | # Now ipshell() will open IPython anywhere in the code | |
1220 | else: |
|
1219 | else: | |
1221 | # Define a dummy ipshell() so the same code doesn't crash inside an |
|
1220 | # Define a dummy ipshell() so the same code doesn't crash inside an | |
1222 | # interactive IPython |
|
1221 | # interactive IPython | |
1223 | def ipshell(): pass |
|
1222 | def ipshell(): pass | |
1224 |
|
1223 | |||
1225 | #******************* End of file <example-embed-short.py> ******************** |
|
1224 | #******************* End of file <example-embed-short.py> ******************** | |
1226 |
|
1225 | |||
1227 | Using the Python debugger (pdb) |
|
1226 | Using the Python debugger (pdb) | |
1228 | =============================== |
|
1227 | =============================== | |
1229 |
|
1228 | |||
1230 | Running entire programs via pdb |
|
1229 | Running entire programs via pdb | |
1231 | ------------------------------- |
|
1230 | ------------------------------- | |
1232 |
|
1231 | |||
1233 | pdb, the Python debugger, is a powerful interactive debugger which |
|
1232 | pdb, the Python debugger, is a powerful interactive debugger which | |
1234 | allows you to step through code, set breakpoints, watch variables, |
|
1233 | allows you to step through code, set breakpoints, watch variables, | |
1235 | etc. IPython makes it very easy to start any script under the control |
|
1234 | etc. IPython makes it very easy to start any script under the control | |
1236 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
1235 | of pdb, regardless of whether you have wrapped it into a 'main()' | |
1237 | function or not. For this, simply type '%run -d myscript' at an |
|
1236 | function or not. For this, simply type '%run -d myscript' at an | |
1238 | IPython prompt. See the %run command's documentation (via '%run?' or |
|
1237 | IPython prompt. See the %run command's documentation (via '%run?' or | |
1239 | in Sec. magic_ for more details, including how to control where pdb |
|
1238 | in Sec. magic_ for more details, including how to control where pdb | |
1240 | will stop execution first. |
|
1239 | will stop execution first. | |
1241 |
|
1240 | |||
1242 | For more information on the use of the pdb debugger, read the included |
|
1241 | For more information on the use of the pdb debugger, read the included | |
1243 | pdb.doc file (part of the standard Python distribution). On a stock |
|
1242 | pdb.doc file (part of the standard Python distribution). On a stock | |
1244 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the |
|
1243 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the | |
1245 | easiest way to read it is by using the help() function of the pdb module |
|
1244 | easiest way to read it is by using the help() function of the pdb module | |
1246 | as follows (in an IPython prompt): |
|
1245 | as follows (in an IPython prompt):: | |
1247 |
|
1246 | |||
1248 |
In [1]: import pdb |
|
1247 | In [1]: import pdb | |
1249 | In [2]: pdb.help() |
|
1248 | In [2]: pdb.help() | |
1250 |
|
1249 | |||
1251 | This will load the pdb.doc document in a file viewer for you automatically. |
|
1250 | This will load the pdb.doc document in a file viewer for you automatically. | |
1252 |
|
1251 | |||
1253 |
|
1252 | |||
1254 | Automatic invocation of pdb on exceptions |
|
1253 | Automatic invocation of pdb on exceptions | |
1255 | ----------------------------------------- |
|
1254 | ----------------------------------------- | |
1256 |
|
1255 | |||
1257 | IPython, if started with the -pdb option (or if the option is set in |
|
1256 | IPython, if started with the -pdb option (or if the option is set in | |
1258 | your rc file) can call the Python pdb debugger every time your code |
|
1257 | your rc file) can call the Python pdb debugger every time your code | |
1259 | triggers an uncaught exception. This feature |
|
1258 | triggers an uncaught exception. This feature | |
1260 | can also be toggled at any time with the %pdb magic command. This can be |
|
1259 | can also be toggled at any time with the %pdb magic command. This can be | |
1261 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
1260 | extremely useful in order to find the origin of subtle bugs, because pdb | |
1262 | opens up at the point in your code which triggered the exception, and |
|
1261 | opens up at the point in your code which triggered the exception, and | |
1263 | while your program is at this point 'dead', all the data is still |
|
1262 | while your program is at this point 'dead', all the data is still | |
1264 | available and you can walk up and down the stack frame and understand |
|
1263 | available and you can walk up and down the stack frame and understand | |
1265 | the origin of the problem. |
|
1264 | the origin of the problem. | |
1266 |
|
1265 | |||
1267 | Furthermore, you can use these debugging facilities both with the |
|
1266 | Furthermore, you can use these debugging facilities both with the | |
1268 | embedded IPython mode and without IPython at all. For an embedded shell |
|
1267 | embedded IPython mode and without IPython at all. For an embedded shell | |
1269 | (see sec. Embedding_), simply call the constructor with |
|
1268 | (see sec. Embedding_), simply call the constructor with | |
1270 | '-pdb' in the argument string and automatically pdb will be called if an |
|
1269 | '-pdb' in the argument string and automatically pdb will be called if an | |
1271 | uncaught exception is triggered by your code. |
|
1270 | uncaught exception is triggered by your code. | |
1272 |
|
1271 | |||
1273 | For stand-alone use of the feature in your programs which do not use |
|
1272 | For stand-alone use of the feature in your programs which do not use | |
1274 | IPython at all, put the following lines toward the top of your 'main' |
|
1273 | IPython at all, put the following lines toward the top of your 'main' | |
1275 | routine:: |
|
1274 | routine:: | |
1276 |
|
1275 | |||
1277 | import sys |
|
1276 | import sys | |
1278 | from IPython.core import ultratb |
|
1277 | from IPython.core import ultratb | |
1279 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
1278 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', | |
1280 | color_scheme='Linux', call_pdb=1) |
|
1279 | color_scheme='Linux', call_pdb=1) | |
1281 |
|
1280 | |||
1282 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
1281 | The mode keyword can be either 'Verbose' or 'Plain', giving either very | |
1283 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
1282 | detailed or normal tracebacks respectively. The color_scheme keyword can | |
1284 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
1283 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same | |
1285 | options which can be set in IPython with -colors and -xmode. |
|
1284 | options which can be set in IPython with -colors and -xmode. | |
1286 |
|
1285 | |||
1287 | This will give any of your programs detailed, colored tracebacks with |
|
1286 | This will give any of your programs detailed, colored tracebacks with | |
1288 | automatic invocation of pdb. |
|
1287 | automatic invocation of pdb. | |
1289 |
|
1288 | |||
1290 |
|
1289 | |||
1291 | Extensions for syntax processing |
|
1290 | Extensions for syntax processing | |
1292 | ================================ |
|
1291 | ================================ | |
1293 |
|
1292 | |||
1294 | This isn't for the faint of heart, because the potential for breaking |
|
1293 | This isn't for the faint of heart, because the potential for breaking | |
1295 | things is quite high. But it can be a very powerful and useful feature. |
|
1294 | things is quite high. But it can be a very powerful and useful feature. | |
1296 | In a nutshell, you can redefine the way IPython processes the user input |
|
1295 | In a nutshell, you can redefine the way IPython processes the user input | |
1297 | line to accept new, special extensions to the syntax without needing to |
|
1296 | line to accept new, special extensions to the syntax without needing to | |
1298 | change any of IPython's own code. |
|
1297 | change any of IPython's own code. | |
1299 |
|
1298 | |||
1300 | In the IPython/extensions directory you will find some examples |
|
1299 | In the IPython/extensions directory you will find some examples | |
1301 | supplied, which we will briefly describe now. These can be used 'as is' |
|
1300 | supplied, which we will briefly describe now. These can be used 'as is' | |
1302 | (and both provide very useful functionality), or you can use them as a |
|
1301 | (and both provide very useful functionality), or you can use them as a | |
1303 | starting point for writing your own extensions. |
|
1302 | starting point for writing your own extensions. | |
1304 |
|
1303 | |||
1305 |
|
1304 | |||
1306 | Pasting of code starting with '>>> ' or '... ' |
|
1305 | Pasting of code starting with '>>> ' or '... ' | |
1307 | ---------------------------------------------- |
|
1306 | ---------------------------------------------- | |
1308 |
|
1307 | |||
1309 | In the python tutorial it is common to find code examples which have |
|
1308 | In the python tutorial it is common to find code examples which have | |
1310 | been taken from real python sessions. The problem with those is that all |
|
1309 | been taken from real python sessions. The problem with those is that all | |
1311 | the lines begin with either '>>> ' or '... ', which makes it impossible |
|
1310 | the lines begin with either '>>> ' or '... ', which makes it impossible | |
1312 | to paste them all at once. One must instead do a line by line manual |
|
1311 | to paste them all at once. One must instead do a line by line manual | |
1313 | copying, carefully removing the leading extraneous characters. |
|
1312 | copying, carefully removing the leading extraneous characters. | |
1314 |
|
1313 | |||
1315 | This extension identifies those starting characters and removes them |
|
1314 | This extension identifies those starting characters and removes them | |
1316 | from the input automatically, so that one can paste multi-line examples |
|
1315 | from the input automatically, so that one can paste multi-line examples | |
1317 | directly into IPython, saving a lot of time. Please look at the file |
|
1316 | directly into IPython, saving a lot of time. Please look at the file | |
1318 | InterpreterPasteInput.py in the IPython/extensions directory for details |
|
1317 | InterpreterPasteInput.py in the IPython/extensions directory for details | |
1319 | on how this is done. |
|
1318 | on how this is done. | |
1320 |
|
1319 | |||
1321 | IPython comes with a special profile enabling this feature, called |
|
1320 | IPython comes with a special profile enabling this feature, called | |
1322 | tutorial. Simply start IPython via 'ipython -p tutorial' and the feature |
|
1321 | tutorial. Simply start IPython via 'ipython -p tutorial' and the feature | |
1323 | will be available. In a normal IPython session you can activate the |
|
1322 | will be available. In a normal IPython session you can activate the | |
1324 | feature by importing the corresponding module with: |
|
1323 | feature by importing the corresponding module with: | |
1325 | In [1]: import IPython.extensions.InterpreterPasteInput |
|
1324 | In [1]: import IPython.extensions.InterpreterPasteInput | |
1326 |
|
1325 | |||
1327 | The following is a 'screenshot' of how things work when this extension |
|
1326 | The following is a 'screenshot' of how things work when this extension | |
1328 | is on, copying an example from the standard tutorial:: |
|
1327 | is on, copying an example from the standard tutorial:: | |
1329 |
|
1328 | |||
1330 | IPython profile: tutorial |
|
1329 | IPython profile: tutorial | |
1331 |
|
1330 | |||
1332 | *** Pasting of code with ">>>" or "..." has been enabled. |
|
1331 | *** Pasting of code with ">>>" or "..." has been enabled. | |
1333 |
|
1332 | |||
1334 | In [1]: >>> def fib2(n): # return Fibonacci series up to n |
|
1333 | In [1]: >>> def fib2(n): # return Fibonacci series up to n | |
1335 | ...: ... """Return a list containing the Fibonacci series up to |
|
1334 | ...: ... """Return a list containing the Fibonacci series up to | |
1336 | n.""" |
|
1335 | n.""" | |
1337 | ...: ... result = [] |
|
1336 | ...: ... result = [] | |
1338 | ...: ... a, b = 0, 1 |
|
1337 | ...: ... a, b = 0, 1 | |
1339 | ...: ... while b < n: |
|
1338 | ...: ... while b < n: | |
1340 | ...: ... result.append(b) # see below |
|
1339 | ...: ... result.append(b) # see below | |
1341 | ...: ... a, b = b, a+b |
|
1340 | ...: ... a, b = b, a+b | |
1342 | ...: ... return result |
|
1341 | ...: ... return result | |
1343 | ...: |
|
1342 | ...: | |
1344 |
|
1343 | |||
1345 | In [2]: fib2(10) |
|
1344 | In [2]: fib2(10) | |
1346 | Out[2]: [1, 1, 2, 3, 5, 8] |
|
1345 | Out[2]: [1, 1, 2, 3, 5, 8] | |
1347 |
|
1346 | |||
1348 | Note that as currently written, this extension does not recognize |
|
1347 | Note that as currently written, this extension does not recognize | |
1349 | IPython's prompts for pasting. Those are more complicated, since the |
|
1348 | IPython's prompts for pasting. Those are more complicated, since the | |
1350 | user can change them very easily, they involve numbers and can vary in |
|
1349 | user can change them very easily, they involve numbers and can vary in | |
1351 | length. One could however extract all the relevant information from the |
|
1350 | length. One could however extract all the relevant information from the | |
1352 | IPython instance and build an appropriate regular expression. This is |
|
1351 | IPython instance and build an appropriate regular expression. This is | |
1353 | left as an exercise for the reader. |
|
1352 | left as an exercise for the reader. | |
1354 |
|
1353 | |||
1355 |
|
1354 | |||
1356 | Input of physical quantities with units |
|
1355 | Input of physical quantities with units | |
1357 | --------------------------------------- |
|
1356 | --------------------------------------- | |
1358 |
|
1357 | |||
1359 | The module PhysicalQInput allows a simplified form of input for physical |
|
1358 | The module PhysicalQInput allows a simplified form of input for physical | |
1360 | quantities with units. This file is meant to be used in conjunction with |
|
1359 | quantities with units. This file is meant to be used in conjunction with | |
1361 | the PhysicalQInteractive module (in the same directory) and |
|
1360 | the PhysicalQInteractive module (in the same directory) and | |
1362 | Physics.PhysicalQuantities from Konrad Hinsen's ScientificPython |
|
1361 | Physics.PhysicalQuantities from Konrad Hinsen's ScientificPython | |
1363 | (http://dirac.cnrs-orleans.fr/ScientificPython/). |
|
1362 | (http://dirac.cnrs-orleans.fr/ScientificPython/). | |
1364 |
|
1363 | |||
1365 | The Physics.PhysicalQuantities module defines PhysicalQuantity objects, |
|
1364 | The Physics.PhysicalQuantities module defines PhysicalQuantity objects, | |
1366 | but these must be declared as instances of a class. For example, to |
|
1365 | but these must be declared as instances of a class. For example, to | |
1367 | define v as a velocity of 3 m/s, normally you would write:: |
|
1366 | define v as a velocity of 3 m/s, normally you would write:: | |
1368 |
|
1367 | |||
1369 | In [1]: v = PhysicalQuantity(3,'m/s') |
|
1368 | In [1]: v = PhysicalQuantity(3,'m/s') | |
1370 |
|
1369 | |||
1371 | Using the PhysicalQ_Input extension this can be input instead as: |
|
1370 | Using the PhysicalQ_Input extension this can be input instead as: | |
1372 | In [1]: v = 3 m/s |
|
1371 | In [1]: v = 3 m/s | |
1373 | which is much more convenient for interactive use (even though it is |
|
1372 | which is much more convenient for interactive use (even though it is | |
1374 | blatantly invalid Python syntax). |
|
1373 | blatantly invalid Python syntax). | |
1375 |
|
1374 | |||
1376 | The physics profile supplied with IPython (enabled via 'ipython -p |
|
1375 | The physics profile supplied with IPython (enabled via 'ipython -p | |
1377 | physics') uses these extensions, which you can also activate with: |
|
1376 | physics') uses these extensions, which you can also activate with: | |
1378 |
|
1377 | |||
1379 | from math import * # math MUST be imported BEFORE PhysicalQInteractive |
|
1378 | from math import * # math MUST be imported BEFORE PhysicalQInteractive | |
1380 | from IPython.extensions.PhysicalQInteractive import * |
|
1379 | from IPython.extensions.PhysicalQInteractive import * | |
1381 | import IPython.extensions.PhysicalQInput |
|
1380 | import IPython.extensions.PhysicalQInput | |
1382 |
|
1381 | |||
1383 | .. _gui_support: |
|
1382 | .. _gui_support: | |
1384 |
|
1383 | |||
1385 | GUI event loop support support |
|
1384 | GUI event loop support support | |
1386 | ============================== |
|
1385 | ============================== | |
1387 |
|
1386 | |||
1388 | .. versionadded:: 0.11 |
|
1387 | .. versionadded:: 0.11 | |
1389 | The ``%gui`` magic and :mod:`IPython.lib.inputhook`. |
|
1388 | The ``%gui`` magic and :mod:`IPython.lib.inputhook`. | |
1390 |
|
1389 | |||
1391 | IPython has excellent support for working interactively with Graphical User |
|
1390 | IPython has excellent support for working interactively with Graphical User | |
1392 | Interface (GUI) toolkits, such as wxPython, PyQt4, PyGTK and Tk. This is |
|
1391 | Interface (GUI) toolkits, such as wxPython, PyQt4, PyGTK and Tk. This is | |
1393 | implemented using Python's builtin ``PyOSInputHook`` hook. This implementation |
|
1392 | implemented using Python's builtin ``PyOSInputHook`` hook. This implementation | |
1394 | is extremely robust compared to our previous threaded based version. The |
|
1393 | is extremely robust compared to our previous threaded based version. The | |
1395 | advantages of this are: |
|
1394 | advantages of this are: | |
1396 |
|
1395 | |||
1397 | * GUIs can be enabled and disabled dynamically at runtime. |
|
1396 | * GUIs can be enabled and disabled dynamically at runtime. | |
1398 | * The active GUI can be switched dynamically at runtime. |
|
1397 | * The active GUI can be switched dynamically at runtime. | |
1399 | * In some cases, multiple GUIs can run simultaneously with no problems. |
|
1398 | * In some cases, multiple GUIs can run simultaneously with no problems. | |
1400 | * There is a developer API in :mod:`IPython.lib.inputhook` for customizing |
|
1399 | * There is a developer API in :mod:`IPython.lib.inputhook` for customizing | |
1401 | all of these things. |
|
1400 | all of these things. | |
1402 |
|
1401 | |||
1403 | For users, enabling GUI event loop integration is simple. You simple use the |
|
1402 | For users, enabling GUI event loop integration is simple. You simple use the | |
1404 | ``%gui`` magic as follows:: |
|
1403 | ``%gui`` magic as follows:: | |
1405 |
|
1404 | |||
1406 | %gui [-a] [GUINAME] |
|
1405 | %gui [-a] [GUINAME] | |
1407 |
|
1406 | |||
1408 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
1407 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` | |
1409 | arguments are ``wx``, ``qt4``, ``gtk`` and ``tk``. The ``-a`` option will |
|
1408 | arguments are ``wx``, ``qt4``, ``gtk`` and ``tk``. The ``-a`` option will | |
1410 | create and return a running application object for the selected GUI toolkit. |
|
1409 | create and return a running application object for the selected GUI toolkit. | |
1411 |
|
1410 | |||
1412 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
1411 | Thus, to use wxPython interactively and create a running :class:`wx.App` | |
1413 | object, do:: |
|
1412 | object, do:: | |
1414 |
|
1413 | |||
1415 | %gui -a wx |
|
1414 | %gui -a wx | |
1416 |
|
1415 | |||
1417 | For information on IPython's Matplotlib integration (and the ``pylab`` mode) |
|
1416 | For information on IPython's Matplotlib integration (and the ``pylab`` mode) | |
1418 | see :ref:`this section <matplotlib_support>`. |
|
1417 | see :ref:`this section <matplotlib_support>`. | |
1419 |
|
1418 | |||
1420 | For developers that want to use IPython's GUI event loop integration in |
|
1419 | For developers that want to use IPython's GUI event loop integration in | |
1421 | the form of a library, these capabilities are exposed in library form |
|
1420 | the form of a library, these capabilities are exposed in library form | |
1422 | in the :mod:`IPython.lib.inputhook`. Interested developers should see the |
|
1421 | in the :mod:`IPython.lib.inputhook`. Interested developers should see the | |
1423 | module docstrings for more information, but there are a few points that |
|
1422 | module docstrings for more information, but there are a few points that | |
1424 | should be mentioned here. |
|
1423 | should be mentioned here. | |
1425 |
|
1424 | |||
1426 | First, the ``PyOSInputHook`` approach only works in command line settings |
|
1425 | First, the ``PyOSInputHook`` approach only works in command line settings | |
1427 | where readline is activated. |
|
1426 | where readline is activated. | |
1428 |
|
1427 | |||
1429 | Second, when using the ``PyOSInputHook`` approach, a GUI application should |
|
1428 | Second, when using the ``PyOSInputHook`` approach, a GUI application should | |
1430 | *not* start its event loop. Instead all of this is handled by the |
|
1429 | *not* start its event loop. Instead all of this is handled by the | |
1431 | ``PyOSInputHook``. This means that applications that are meant to be used both |
|
1430 | ``PyOSInputHook``. This means that applications that are meant to be used both | |
1432 | in IPython and as standalone apps need to have special code to detects how the |
|
1431 | in IPython and as standalone apps need to have special code to detects how the | |
1433 | application is being run. We highly recommend using IPython's |
|
1432 | application is being run. We highly recommend using IPython's | |
1434 | :func:`appstart_` functions for this. Here is a simple example that shows the |
|
1433 | :func:`appstart_` functions for this. Here is a simple example that shows the | |
1435 | recommended code that should be at the bottom of a wxPython using GUI |
|
1434 | recommended code that should be at the bottom of a wxPython using GUI | |
1436 | application:: |
|
1435 | application:: | |
1437 |
|
1436 | |||
1438 | try: |
|
1437 | try: | |
1439 | from IPython import appstart_wx |
|
1438 | from IPython import appstart_wx | |
1440 | appstart_wx(app) |
|
1439 | appstart_wx(app) | |
1441 | except ImportError: |
|
1440 | except ImportError: | |
1442 | app.MainLoop() |
|
1441 | app.MainLoop() | |
1443 |
|
1442 | |||
1444 | This pattern should be used instead of the simple ``app.MainLoop()`` code |
|
1443 | This pattern should be used instead of the simple ``app.MainLoop()`` code | |
1445 | that a standalone wxPython application would have. |
|
1444 | that a standalone wxPython application would have. | |
1446 |
|
1445 | |||
1447 | Third, unlike previous versions of IPython, we no longer "hijack" (replace |
|
1446 | Third, unlike previous versions of IPython, we no longer "hijack" (replace | |
1448 | them with no-ops) the event loops. This is done to allow applications that |
|
1447 | them with no-ops) the event loops. This is done to allow applications that | |
1449 | actually need to run the real event loops to do so. This is often needed to |
|
1448 | actually need to run the real event loops to do so. This is often needed to | |
1450 | process pending events at critical points. |
|
1449 | process pending events at critical points. | |
1451 |
|
1450 | |||
1452 | Finally, we also have a number of examples in our source directory |
|
1451 | Finally, we also have a number of examples in our source directory | |
1453 | :file:`docs/examples/lib` that demonstrate these capabilities. |
|
1452 | :file:`docs/examples/lib` that demonstrate these capabilities. | |
1454 |
|
1453 | |||
1455 | .. _matplotlib_support: |
|
1454 | .. _matplotlib_support: | |
1456 |
|
1455 | |||
1457 | Plotting with matplotlib |
|
1456 | Plotting with matplotlib | |
1458 | ======================== |
|
1457 | ======================== | |
1459 |
|
1458 | |||
1460 |
|
1459 | |||
1461 | `Matplotlib`_ provides high quality 2D and |
|
1460 | `Matplotlib`_ provides high quality 2D and | |
1462 | 3D plotting for Python. Matplotlib can produce plots on screen using a variety |
|
1461 | 3D plotting for Python. Matplotlib can produce plots on screen using a variety | |
1463 | of GUI toolkits, including Tk, PyGTK, PyQt4 and wxPython. It also provides a |
|
1462 | of GUI toolkits, including Tk, PyGTK, PyQt4 and wxPython. It also provides a | |
1464 | number of commands useful for scientific computing, all with a syntax |
|
1463 | number of commands useful for scientific computing, all with a syntax | |
1465 | compatible with that of the popular Matlab program. |
|
1464 | compatible with that of the popular Matlab program. | |
1466 |
|
1465 | |||
1467 | Many IPython users have come to rely on IPython's ``-pylab`` mode which |
|
1466 | Many IPython users have come to rely on IPython's ``-pylab`` mode which | |
1468 | automates the integration of Matplotlib with IPython. We are still in the |
|
1467 | automates the integration of Matplotlib with IPython. We are still in the | |
1469 | process of working with the Matplotlib developers to finalize the new pylab |
|
1468 | process of working with the Matplotlib developers to finalize the new pylab | |
1470 | API, but for now you can use Matplotlib interactively using the following |
|
1469 | API, but for now you can use Matplotlib interactively using the following | |
1471 | commands:: |
|
1470 | commands:: | |
1472 |
|
1471 | |||
1473 | %gui -a wx |
|
1472 | %gui -a wx | |
1474 | import matplotlib |
|
1473 | import matplotlib | |
1475 | matplotlib.use('wxagg') |
|
1474 | matplotlib.use('wxagg') | |
1476 | from matplotlib import pylab |
|
1475 | from matplotlib import pylab | |
1477 | pylab.interactive(True) |
|
1476 | pylab.interactive(True) | |
1478 |
|
1477 | |||
1479 |
All of this will soon be automated as Matplotlib bein |
|
1478 | All of this will soon be automated as Matplotlib begins to include | |
1480 | new logic that uses our new GUI support. |
|
1479 | new logic that uses our new GUI support. | |
1481 |
|
1480 | |||
|
1481 | .. _Matplotlib: http://matplotlib.sourceforge.net | |||
|
1482 | ||||
1482 | .. _interactive_demos: |
|
1483 | .. _interactive_demos: | |
1483 |
|
1484 | |||
1484 | Interactive demos with IPython |
|
1485 | Interactive demos with IPython | |
1485 | ============================== |
|
1486 | ============================== | |
1486 |
|
1487 | |||
1487 | IPython ships with a basic system for running scripts interactively in |
|
1488 | IPython ships with a basic system for running scripts interactively in | |
1488 | sections, useful when presenting code to audiences. A few tags embedded |
|
1489 | sections, useful when presenting code to audiences. A few tags embedded | |
1489 | in comments (so that the script remains valid Python code) divide a file |
|
1490 | in comments (so that the script remains valid Python code) divide a file | |
1490 | into separate blocks, and the demo can be run one block at a time, with |
|
1491 | into separate blocks, and the demo can be run one block at a time, with | |
1491 | IPython printing (with syntax highlighting) the block before executing |
|
1492 | IPython printing (with syntax highlighting) the block before executing | |
1492 | it, and returning to the interactive prompt after each block. The |
|
1493 | it, and returning to the interactive prompt after each block. The | |
1493 | interactive namespace is updated after each block is run with the |
|
1494 | interactive namespace is updated after each block is run with the | |
1494 | contents of the demo's namespace. |
|
1495 | contents of the demo's namespace. | |
1495 |
|
1496 | |||
1496 | This allows you to show a piece of code, run it and then execute |
|
1497 | This allows you to show a piece of code, run it and then execute | |
1497 | interactively commands based on the variables just created. Once you |
|
1498 | interactively commands based on the variables just created. Once you | |
1498 | want to continue, you simply execute the next block of the demo. The |
|
1499 | want to continue, you simply execute the next block of the demo. The | |
1499 | following listing shows the markup necessary for dividing a script into |
|
1500 | following listing shows the markup necessary for dividing a script into | |
1500 | sections for execution as a demo:: |
|
1501 | sections for execution as a demo:: | |
1501 |
|
1502 | |||
1502 |
|
1503 | |||
1503 | """A simple interactive demo to illustrate the use of IPython's Demo class. |
|
1504 | """A simple interactive demo to illustrate the use of IPython's Demo class. | |
1504 |
|
1505 | |||
1505 | Any python script can be run as a demo, but that does little more than showing |
|
1506 | Any python script can be run as a demo, but that does little more than showing | |
1506 | it on-screen, syntax-highlighted in one shot. If you add a little simple |
|
1507 | it on-screen, syntax-highlighted in one shot. If you add a little simple | |
1507 | markup, you can stop at specified intervals and return to the ipython prompt, |
|
1508 | markup, you can stop at specified intervals and return to the ipython prompt, | |
1508 | resuming execution later. |
|
1509 | resuming execution later. | |
1509 | """ |
|
1510 | """ | |
1510 |
|
1511 | |||
1511 | print 'Hello, welcome to an interactive IPython demo.' |
|
1512 | print 'Hello, welcome to an interactive IPython demo.' | |
1512 | print 'Executing this block should require confirmation before proceeding,' |
|
1513 | print 'Executing this block should require confirmation before proceeding,' | |
1513 | print 'unless auto_all has been set to true in the demo object' |
|
1514 | print 'unless auto_all has been set to true in the demo object' | |
1514 |
|
1515 | |||
1515 | # The mark below defines a block boundary, which is a point where IPython will |
|
1516 | # The mark below defines a block boundary, which is a point where IPython will | |
1516 | # stop execution and return to the interactive prompt. |
|
1517 | # stop execution and return to the interactive prompt. | |
1517 | # Note that in actual interactive execution, |
|
1518 | # Note that in actual interactive execution, | |
1518 | # <demo> --- stop --- |
|
1519 | # <demo> --- stop --- | |
1519 |
|
1520 | |||
1520 | x = 1 |
|
1521 | x = 1 | |
1521 | y = 2 |
|
1522 | y = 2 | |
1522 |
|
1523 | |||
1523 | # <demo> --- stop --- |
|
1524 | # <demo> --- stop --- | |
1524 |
|
1525 | |||
1525 | # the mark below makes this block as silent |
|
1526 | # the mark below makes this block as silent | |
1526 | # <demo> silent |
|
1527 | # <demo> silent | |
1527 |
|
1528 | |||
1528 | print 'This is a silent block, which gets executed but not printed.' |
|
1529 | print 'This is a silent block, which gets executed but not printed.' | |
1529 |
|
1530 | |||
1530 | # <demo> --- stop --- |
|
1531 | # <demo> --- stop --- | |
1531 | # <demo> auto |
|
1532 | # <demo> auto | |
1532 | print 'This is an automatic block.' |
|
1533 | print 'This is an automatic block.' | |
1533 | print 'It is executed without asking for confirmation, but printed.' |
|
1534 | print 'It is executed without asking for confirmation, but printed.' | |
1534 | z = x+y |
|
1535 | z = x+y | |
1535 |
|
1536 | |||
1536 | print 'z=',x |
|
1537 | print 'z=',x | |
1537 |
|
1538 | |||
1538 | # <demo> --- stop --- |
|
1539 | # <demo> --- stop --- | |
1539 | # This is just another normal block. |
|
1540 | # This is just another normal block. | |
1540 | print 'z is now:', z |
|
1541 | print 'z is now:', z | |
1541 |
|
1542 | |||
1542 | print 'bye!' |
|
1543 | print 'bye!' | |
1543 |
|
1544 | |||
1544 | In order to run a file as a demo, you must first make a Demo object out |
|
1545 | In order to run a file as a demo, you must first make a Demo object out | |
1545 | of it. If the file is named myscript.py, the following code will make a |
|
1546 | of it. If the file is named myscript.py, the following code will make a | |
1546 | demo:: |
|
1547 | demo:: | |
1547 |
|
1548 | |||
1548 | from IPython.demo import Demo |
|
1549 | from IPython.lib.demo import Demo | |
1549 |
|
1550 | |||
1550 | mydemo = Demo('myscript.py') |
|
1551 | mydemo = Demo('myscript.py') | |
1551 |
|
1552 | |||
1552 | This creates the mydemo object, whose blocks you run one at a time by |
|
1553 | This creates the mydemo object, whose blocks you run one at a time by | |
1553 | simply calling the object with no arguments. If you have autocall active |
|
1554 | simply calling the object with no arguments. If you have autocall active | |
1554 | in IPython (the default), all you need to do is type:: |
|
1555 | in IPython (the default), all you need to do is type:: | |
1555 |
|
1556 | |||
1556 | mydemo |
|
1557 | mydemo | |
1557 |
|
1558 | |||
1558 | and IPython will call it, executing each block. Demo objects can be |
|
1559 | and IPython will call it, executing each block. Demo objects can be | |
1559 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1560 | restarted, you can move forward or back skipping blocks, re-execute the | |
1560 | last block, etc. Simply use the Tab key on a demo object to see its |
|
1561 | last block, etc. Simply use the Tab key on a demo object to see its | |
1561 | methods, and call '?' on them to see their docstrings for more usage |
|
1562 | methods, and call '?' on them to see their docstrings for more usage | |
1562 | details. In addition, the demo module itself contains a comprehensive |
|
1563 | details. In addition, the demo module itself contains a comprehensive | |
1563 | docstring, which you can access via:: |
|
1564 | docstring, which you can access via:: | |
1564 |
|
1565 | |||
1565 | from IPython import demo |
|
1566 | from IPython.lib import demo | |
1566 |
|
1567 | |||
1567 | demo? |
|
1568 | demo? | |
1568 |
|
1569 | |||
1569 | Limitations: It is important to note that these demos are limited to |
|
1570 | Limitations: It is important to note that these demos are limited to | |
1570 | fairly simple uses. In particular, you can not put division marks in |
|
1571 | fairly simple uses. In particular, you can not put division marks in | |
1571 | indented code (loops, if statements, function definitions, etc.) |
|
1572 | indented code (loops, if statements, function definitions, etc.) | |
1572 | Supporting something like this would basically require tracking the |
|
1573 | Supporting something like this would basically require tracking the | |
1573 | internal execution state of the Python interpreter, so only top-level |
|
1574 | internal execution state of the Python interpreter, so only top-level | |
1574 | divisions are allowed. If you want to be able to open an IPython |
|
1575 | divisions are allowed. If you want to be able to open an IPython | |
1575 | instance at an arbitrary point in a program, you can use IPython's |
|
1576 | instance at an arbitrary point in a program, you can use IPython's | |
1576 | embedding facilities, described in detail in Sec. 9 |
|
1577 | embedding facilities, described in detail in Sec. 9 | |
1577 |
|
1578 | |||
1578 | .. [Matplotlib] Matplotlib. http://matplotlib.sourceforge.net |
|
|||
1579 |
|
@@ -1,129 +1,131 b'' | |||||
1 | .. _tips: |
|
1 | .. _tips: | |
2 |
|
2 | |||
3 | ===================== |
|
3 | ===================== | |
4 | IPython Tips & Tricks |
|
4 | IPython Tips & Tricks | |
5 | ===================== |
|
5 | ===================== | |
6 |
|
6 | |||
7 | The `IPython cookbook <http://ipython.scipy.org/moin/Cookbook>`_ details more |
|
7 | The `IPython cookbook <http://ipython.scipy.org/moin/Cookbook>`_ details more | |
8 | things you can do with IPython. |
|
8 | things you can do with IPython. | |
9 |
|
9 | |||
10 | .. This is not in the current version: |
|
10 | .. This is not in the current version: | |
11 | .. Use IPython to present interactive demos |
|
|||
12 | ---------------------------------------- |
|
|||
13 |
|
11 | |||
14 | .. Use the IPython.demo.Demo class to load any Python script as an interactive |
|
|||
15 | demo. With a minimal amount of simple markup, you can control the execution of |
|
|||
16 | the script, stopping as needed. See :ref:`here <interactive_demos>` for more. |
|
|||
17 |
|
12 | |||
18 | Embed IPython in your programs |
|
13 | Embed IPython in your programs | |
19 | ------------------------------ |
|
14 | ------------------------------ | |
20 |
|
15 | |||
21 | A few lines of code are enough to load a complete IPython inside your own |
|
16 | A few lines of code are enough to load a complete IPython inside your own | |
22 | programs, giving you the ability to work with your data interactively after |
|
17 | programs, giving you the ability to work with your data interactively after | |
23 | automatic processing has been completed. See :ref:`the embedding section <embedding>`. |
|
18 | automatic processing has been completed. See :ref:`the embedding section <embedding>`. | |
24 |
|
19 | |||
25 | Run doctests |
|
20 | Run doctests | |
26 | ------------ |
|
21 | ------------ | |
27 |
|
22 | |||
28 | Run your doctests from within IPython for development and debugging. The |
|
23 | Run your doctests from within IPython for development and debugging. The | |
29 | special %doctest_mode command toggles a mode where the prompt, output and |
|
24 | special %doctest_mode command toggles a mode where the prompt, output and | |
30 | exceptions display matches as closely as possible that of the default Python |
|
25 | exceptions display matches as closely as possible that of the default Python | |
31 | interpreter. In addition, this mode allows you to directly paste in code that |
|
26 | interpreter. In addition, this mode allows you to directly paste in code that | |
32 | contains leading '>>>' prompts, even if they have extra leading whitespace |
|
27 | contains leading '>>>' prompts, even if they have extra leading whitespace | |
33 | (as is common in doctest files). This combined with the ``%history -t`` call |
|
28 | (as is common in doctest files). This combined with the ``%history -t`` call | |
34 | to see your translated history allows for an easy doctest workflow, where you |
|
29 | to see your translated history allows for an easy doctest workflow, where you | |
35 | can go from doctest to interactive execution to pasting into valid Python code |
|
30 | can go from doctest to interactive execution to pasting into valid Python code | |
36 | as needed. |
|
31 | as needed. | |
37 |
|
32 | |||
|
33 | Use IPython to present interactive demos | |||
|
34 | ---------------------------------------- | |||
|
35 | ||||
|
36 | Use the :class:`IPython.lib.demo.Demo` class to load any Python script as an interactive | |||
|
37 | demo. With a minimal amount of simple markup, you can control the execution of | |||
|
38 | the script, stopping as needed. See :ref:`here <interactive_demos>` for more. | |||
|
39 | ||||
38 | Suppress output |
|
40 | Suppress output | |
39 | --------------- |
|
41 | --------------- | |
40 |
|
42 | |||
41 | Put a ';' at the end of a line to suppress the printing of output. This is |
|
43 | Put a ';' at the end of a line to suppress the printing of output. This is | |
42 | useful when doing calculations which generate long output you are not |
|
44 | useful when doing calculations which generate long output you are not | |
43 | interested in seeing. |
|
45 | interested in seeing. | |
44 |
|
46 | |||
45 | Lightweight 'version control' |
|
47 | Lightweight 'version control' | |
46 | ----------------------------- |
|
48 | ----------------------------- | |
47 |
|
49 | |||
48 | When you call ``%edit`` with no arguments, IPython opens an empty editor |
|
50 | When you call ``%edit`` with no arguments, IPython opens an empty editor | |
49 | with a temporary file, and it returns the contents of your editing |
|
51 | with a temporary file, and it returns the contents of your editing | |
50 | session as a string variable. Thanks to IPython's output caching |
|
52 | session as a string variable. Thanks to IPython's output caching | |
51 | mechanism, this is automatically stored:: |
|
53 | mechanism, this is automatically stored:: | |
52 |
|
54 | |||
53 | In [1]: %edit |
|
55 | In [1]: %edit | |
54 |
|
56 | |||
55 | IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py |
|
57 | IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py | |
56 |
|
58 | |||
57 | Editing... done. Executing edited code... |
|
59 | Editing... done. Executing edited code... | |
58 |
|
60 | |||
59 | hello - this is a temporary file |
|
61 | hello - this is a temporary file | |
60 |
|
62 | |||
61 | Out[1]: "print 'hello - this is a temporary file'\n" |
|
63 | Out[1]: "print 'hello - this is a temporary file'\n" | |
62 |
|
64 | |||
63 | Now, if you call ``%edit -p``, IPython tries to open an editor with the |
|
65 | Now, if you call ``%edit -p``, IPython tries to open an editor with the | |
64 | same data as the last time you used %edit. So if you haven't used %edit |
|
66 | same data as the last time you used %edit. So if you haven't used %edit | |
65 | in the meantime, this same contents will reopen; however, it will be |
|
67 | in the meantime, this same contents will reopen; however, it will be | |
66 | done in a new file. This means that if you make changes and you later |
|
68 | done in a new file. This means that if you make changes and you later | |
67 | want to find an old version, you can always retrieve it by using its |
|
69 | want to find an old version, you can always retrieve it by using its | |
68 | output number, via '%edit _NN', where NN is the number of the output |
|
70 | output number, via '%edit _NN', where NN is the number of the output | |
69 | prompt. |
|
71 | prompt. | |
70 |
|
72 | |||
71 | Continuing with the example above, this should illustrate this idea:: |
|
73 | Continuing with the example above, this should illustrate this idea:: | |
72 |
|
74 | |||
73 | In [2]: edit -p |
|
75 | In [2]: edit -p | |
74 |
|
76 | |||
75 | IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py |
|
77 | IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py | |
76 |
|
78 | |||
77 | Editing... done. Executing edited code... |
|
79 | Editing... done. Executing edited code... | |
78 |
|
80 | |||
79 | hello - now I made some changes |
|
81 | hello - now I made some changes | |
80 |
|
82 | |||
81 | Out[2]: "print 'hello - now I made some changes'\n" |
|
83 | Out[2]: "print 'hello - now I made some changes'\n" | |
82 |
|
84 | |||
83 | In [3]: edit _1 |
|
85 | In [3]: edit _1 | |
84 |
|
86 | |||
85 | IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py |
|
87 | IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py | |
86 |
|
88 | |||
87 | Editing... done. Executing edited code... |
|
89 | Editing... done. Executing edited code... | |
88 |
|
90 | |||
89 | hello - this is a temporary file |
|
91 | hello - this is a temporary file | |
90 |
|
92 | |||
91 | IPython version control at work :) |
|
93 | IPython version control at work :) | |
92 |
|
94 | |||
93 | Out[3]: "print 'hello - this is a temporary file'\nprint 'IPython version control at work :)'\n" |
|
95 | Out[3]: "print 'hello - this is a temporary file'\nprint 'IPython version control at work :)'\n" | |
94 |
|
96 | |||
95 |
|
97 | |||
96 | This section was written after a contribution by Alexander Belchenko on |
|
98 | This section was written after a contribution by Alexander Belchenko on | |
97 | the IPython user list. |
|
99 | the IPython user list. | |
98 |
|
100 | |||
99 | .. The section below needs to be updated for the new config system. |
|
101 | .. The section below needs to be updated for the new config system. | |
100 |
|
102 | |||
101 | .. Effective logging |
|
103 | .. Effective logging | |
102 | ----------------- |
|
104 | ----------------- | |
103 |
|
105 | |||
104 | .. A very useful suggestion sent in by Robert Kern follows: |
|
106 | .. A very useful suggestion sent in by Robert Kern follows: | |
105 |
|
107 | |||
106 | .. I recently happened on a nifty way to keep tidy per-project log files. I |
|
108 | .. I recently happened on a nifty way to keep tidy per-project log files. I | |
107 | made a profile for my project (which is called "parkfield"):: |
|
109 | made a profile for my project (which is called "parkfield"):: | |
108 |
|
110 | |||
109 | include ipythonrc |
|
111 | include ipythonrc | |
110 |
|
112 | |||
111 | # cancel earlier logfile invocation: |
|
113 | # cancel earlier logfile invocation: | |
112 |
|
114 | |||
113 | logfile '' |
|
115 | logfile '' | |
114 |
|
116 | |||
115 | execute import time |
|
117 | execute import time | |
116 |
|
118 | |||
117 | execute __cmd = '/Users/kern/research/logfiles/parkfield-%s.log rotate' |
|
119 | execute __cmd = '/Users/kern/research/logfiles/parkfield-%s.log rotate' | |
118 |
|
120 | |||
119 | execute __IP.magic_logstart(__cmd % time.strftime('%Y-%m-%d')) |
|
121 | execute __IP.magic_logstart(__cmd % time.strftime('%Y-%m-%d')) | |
120 |
|
122 | |||
121 | .. I also added a shell alias for convenience:: |
|
123 | .. I also added a shell alias for convenience:: | |
122 |
|
124 | |||
123 | alias parkfield="ipython --pylab profile=parkfield" |
|
125 | alias parkfield="ipython --pylab profile=parkfield" | |
124 |
|
126 | |||
125 | .. Now I have a nice little directory with everything I ever type in, |
|
127 | .. Now I have a nice little directory with everything I ever type in, | |
126 | organized by project and date. |
|
128 | organized by project and date. | |
127 |
|
129 | |||
128 |
|
130 | |||
129 |
|
131 |
General Comments 0
You need to be logged in to leave comments.
Login now