~ubuntu-branches/ubuntu/raring/ipython/raring

« back to all changes in this revision

Viewing changes to docs/source/interactive/tutorial.txt

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2011-11-22 23:40:57 UTC
  • mfrom: (6.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20111122234057-ta86ocdahnhwmnd8
Tags: 0.11-2
* upload to unstable
* add patch fix-version-checks-for-pyzmq-2.1.10.patch
* fix debianize-error-messages.patch to reraise unknown exceptions
* suggest python-zmq for ipython package
* use dh_sphinxdoc
  - bump sphinx dependency to >= 1.0.7+dfsg-1~, replace libjs-jquery
    dependency with ${sphinxdoc:Depends} and drop ipython-doc.links
* remove empty directory from ipython
* link duplicate images in ipython-doc
* remove obsolete Conflicts and Replaces

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
.. _tutorial:
2
2
 
3
3
======================
4
 
Quick IPython tutorial
 
4
Introducing IPython
5
5
======================
6
6
 
7
 
IPython can be used as an improved replacement for the Python prompt,
8
 
and for that you don't really need to read any more of this manual. But
9
 
in this section we'll try to summarize a few tips on how to make the
10
 
most effective use of it for everyday Python development, highlighting
11
 
things you might miss in the rest of the manual (which is getting long).
12
 
We'll give references to parts in the manual which provide more detail
13
 
when appropriate.
14
 
 
15
 
The following article by Jeremy Jones provides an introductory tutorial
16
 
about IPython: http://www.onlamp.com/pub/a/python/2005/01/27/ipython.html
17
 
 
18
 
Highlights
19
 
==========
 
7
You don't need to know anything beyond Python to start using IPython – just type
 
8
commands as you would at the standard Python prompt. But IPython can do much
 
9
more than the standard prompt. Some key features are described here. For more
 
10
information, check the :ref:`tips page <tips>`, or look at examples in the
 
11
`IPython cookbook <http://ipython.scipy.org/moin/Cookbook>`_.
 
12
 
 
13
If you've never used Python before, you might want to look at `the official
 
14
tutorial <http://docs.python.org/tutorial/>`_ or an alternative, `Dive into
 
15
Python <http://diveintopython.org/toc/index.html>`_.
20
16
 
21
17
Tab completion
22
 
--------------
23
 
 
24
 
TAB-completion, especially for attributes, is a convenient way to explore the
25
 
structure of any object you're dealing with. Simply type object_name.<TAB> and
26
 
a list of the object's attributes will be printed (see :ref:`the readline
27
 
section <readline>` for more). Tab completion also works on file and directory
28
 
names, which combined with IPython's alias system allows you to do from within
29
 
IPython many of the things you normally would need the system shell for.
30
 
 
31
 
Explore your objects
32
 
--------------------
33
 
 
34
 
Typing object_name? will print all sorts of details about any object,
 
18
==============
 
19
 
 
20
Tab completion, especially for attributes, is a convenient way to explore the
 
21
structure of any object you're dealing with. Simply type ``object_name.<TAB>``
 
22
to view the object's attributes (see :ref:`the readline section <readline>` for
 
23
more). Besides Python objects and keywords, tab completion also works on file
 
24
and directory names.
 
25
 
 
26
Exploring your objects
 
27
======================
 
28
 
 
29
Typing ``object_name?`` will print all sorts of details about any object,
35
30
including docstrings, function definition lines (for call arguments) and
36
 
constructor details for classes. The magic commands %pdoc, %pdef, %psource
37
 
and %pfile will respectively print the docstring, function definition line,
38
 
full source code and the complete file for any object (when they can be
39
 
found). If automagic is on (it is by default), you don't need to type the '%'
40
 
explicitly. See :ref:`this section <dynamic_object_info>` for more.
41
 
 
42
 
The `%run` magic command
43
 
------------------------
 
31
constructor details for classes. To get specific information on an object, you
 
32
can use the magic commands ``%pdoc``, ``%pdef``, ``%psource`` and ``%pfile``
 
33
 
 
34
Magic functions
 
35
===============
 
36
 
 
37
IPython has a set of predefined 'magic functions' that you can call with a
 
38
command line style syntax. These include:
 
39
 
 
40
- Functions that work with code: ``%run``, ``%edit``, ``%save``, ``%macro``,
 
41
  ``%recall``, etc.
 
42
- Functions which affect the shell: ``%colors``, ``%xmode``, ``%autoindent``, etc.
 
43
- Other functions such as ``%reset``, ``%timeit`` or ``%paste``.
 
44
 
 
45
You can always call these using the % prefix, and if you're typing one on a line
 
46
by itself, you can omit even that::
 
47
 
 
48
    run thescript.py
 
49
    
 
50
For more details on any magic function, call ``%somemagic?`` to read its
 
51
docstring. To see all the available magic functions, call ``%lsmagic``.
 
52
 
 
53
Running and Editing
 
54
-------------------
44
55
 
45
56
The %run magic command allows you to run any python script and load all of its
46
57
data directly into the interactive namespace. Since the file is re-read from
47
 
disk each time, changes you make to it are reflected immediately (in contrast
48
 
to the behavior of import). I rarely use import for code I am testing, relying
49
 
on %run instead. See :ref:`this section <magic>` for more on this and other
50
 
magic commands, or type the name of any magic command and ? to get details on
51
 
it. See also :ref:`this section <dreload>` for a recursive reload command. %run
52
 
also has special flags for timing the execution of your scripts (-t) and for
53
 
executing them under the control of either Python's pdb debugger (-d) or
54
 
profiler (-p). With all of these, %run can be used as the main tool for
55
 
efficient interactive development of code which you write in your editor of
56
 
choice.
57
 
 
58
 
Debug a Python script
59
 
---------------------
60
 
 
61
 
Use the Python debugger, pdb. The %pdb command allows you to toggle on and off
62
 
the automatic invocation of an IPython-enhanced pdb debugger (with coloring,
63
 
tab completion and more) at any uncaught exception. The advantage of this is
64
 
that pdb starts inside the function where the exception occurred, with all data
65
 
still available. You can print variables, see code, execute statements and even
66
 
walk up and down the call stack to track down the true source of the problem
67
 
(which often is many layers in the stack above where the exception gets
68
 
triggered). Running programs with %run and pdb active can be an efficient to
69
 
develop and debug code, in many cases eliminating the need for print statements
70
 
or external debugging tools. I often simply put a 1/0 in a place where I want
71
 
to take a look so that pdb gets called, quickly view whatever variables I need
72
 
to or test various pieces of code and then remove the 1/0. Note also that '%run
73
 
-d' activates pdb and automatically sets initial breakpoints for you to step
74
 
through your code, watch variables, etc.  The :ref:`output caching section
75
 
<output_caching>` has more details.
76
 
 
77
 
Use the output cache
78
 
--------------------
79
 
 
80
 
All output results are automatically stored in a global dictionary named Out
81
 
and variables named _1, _2, etc. alias them. For example, the result of input
82
 
line 4 is available either as Out[4] or as _4. Additionally, three variables
83
 
named _, __ and ___ are always kept updated with the for the last three
84
 
results. This allows you to recall any previous result and further use it for
85
 
new calculations. See :ref:`the output caching section <output_caching>` for
86
 
more.
87
 
 
88
 
Suppress output
89
 
---------------
90
 
 
91
 
Put a ';' at the end of a line to suppress the printing of output. This is
92
 
useful when doing calculations which generate long output you are not
93
 
interested in seeing. The _* variables and the Out[] list do get updated with
94
 
the contents of the output, even if it is not printed. You can thus still
95
 
access the generated results this way for further processing.
96
 
 
97
 
Input cache
98
 
-----------
99
 
 
100
 
A similar system exists for caching input. All input is stored in a global
101
 
list called In , so you can re-execute lines 22 through 28 plus line 34 by
102
 
typing 'exec In[22:29]+In[34]' (using Python slicing notation). If you need
103
 
to execute the same set of lines often, you can assign them to a macro with
104
 
the %macro function. See :ref:`here <input_caching>` for more.
105
 
 
106
 
Use your input history
107
 
----------------------
108
 
 
109
 
The %hist command can show you all previous input, without line numbers if
110
 
desired (option -n) so you can directly copy and paste code either back in
111
 
IPython or in a text editor. You can also save all your history by turning on
112
 
logging via %logstart; these logs can later be either reloaded as IPython
113
 
sessions or used as code for your programs.
114
 
 
 
58
disk each time, changes you make to it are reflected immediately (unlike
 
59
imported modules, which have to be specifically reloaded). IPython also includes
 
60
:ref:`dreload <dreload>`, a recursive reload function.
 
61
 
 
62
%run has special flags for timing the execution of your scripts (-t), or for
 
63
running them under the control of either Python's pdb debugger (-d) or
 
64
profiler (-p).
 
65
 
 
66
The %edit command gives a reasonable approximation of multiline editing,
 
67
by invoking your favorite editor on the spot. IPython will execute the
 
68
code you type in there as if it were typed interactively.
 
69
 
 
70
Debugging
 
71
---------
 
72
 
 
73
After an exception occurs, you can call ``%debug`` to jump into the Python
 
74
debugger (pdb) and examine the problem. Alternatively, if you call ``%pdb``,
 
75
IPython will automatically start the debugger on any uncaught exception. You can
 
76
print variables, see code, execute statements and even walk up and down the
 
77
call stack to track down the true source of the problem. Running programs with
 
78
%run and pdb active can be an efficient way to develop and debug code, in many
 
79
cases eliminating the need for print statements or external debugging tools.
 
80
 
 
81
You can also step through a program from the beginning by calling
 
82
``%run -d theprogram.py``.
 
83
 
 
84
History
 
85
=======
 
86
 
 
87
IPython stores both the commands you enter, and the results it produces. You
 
88
can easily go through previous commands with the up- and down-arrow keys, or
 
89
access your history in more sophisticated ways.
 
90
 
 
91
Input and output history are kept in variables called ``In`` and ``Out``, which
 
92
can both be indexed by the prompt number on which they occurred, e.g. ``In[4]``.
 
93
The last three objects in output history are also kept in variables named ``_``,
 
94
``__`` and ``___``.
 
95
 
 
96
You can use the ``%history`` magic function to examine past input and output.
 
97
Input history from previous sessions is saved in a database, and IPython can be
 
98
configured to save output history.
 
99
 
 
100
Several other magic functions can use your input history, including ``%edit``, 
 
101
``%rerun``, ``%recall``, ``%macro``, ``%save`` and ``%pastebin``. You can use a
 
102
standard format to refer to lines::
 
103
 
 
104
    %pastebin 3 18-20 ~1/1-5
 
105
    
 
106
This will take line 3 and lines 18 to 20 from the current session, and lines
 
107
1-5 from the previous session.
 
108
 
 
109
System shell commands
 
110
=====================
 
111
 
 
112
To run any command at the system shell, simply prefix it with !, e.g.::
 
113
 
 
114
    !ping www.bbc.co.uk
 
115
    
 
116
You can capture the output into a Python list, e.g.: ``files = !ls``. To pass
 
117
the values of Python variables or expressions to system commands, prefix them
 
118
with $: ``!grep -rF $pattern ipython/*``. See :ref:`our shell section
 
119
<system_shell_access>` for more details.
 
120
        
115
121
Define your own system aliases
116
122
------------------------------
117
123
 
118
 
Even though IPython gives you access to your system shell via the ! prefix,
119
 
it is convenient to have aliases to the system commands you use most often.
 
124
It's convenient to have aliases to the system commands you use most often.
120
125
This allows you to work seamlessly from inside IPython with the same commands
121
126
you are used to in your system shell. IPython comes with some pre-defined
122
127
aliases and a complete system for changing directories, both via a stack (see
123
128
%pushd, %popd and %dhist) and via direct %cd. The latter keeps a history of
124
129
visited directories and allows you to go to any previously visited one.
125
130
 
126
 
Call system shell commands
127
 
--------------------------
128
 
 
129
 
Use Python to manipulate the results of system commands. The '!!' special
130
 
syntax, and the %sc and %sx magic commands allow you to capture system output
131
 
into Python variables.
132
 
 
133
 
Use Python variables when calling the shell
134
 
-------------------------------------------
135
 
 
136
 
Expand python variables when calling the shell (either via '!' and '!!' or via
137
 
aliases) by prepending a $ in front of them. You can also expand complete
138
 
python expressions. See :ref:`our shell section <system_shell_access>` for
139
 
more details.
140
 
 
141
 
Use profiles
142
 
------------
143
 
 
144
 
Use profiles to maintain different configurations (modules to load, function
145
 
definitions, option settings) for particular tasks. You can then have
146
 
customized versions of IPython for specific purposes. :ref:`This section
147
 
<profiles>` has more details.
148
 
 
149
 
 
150
 
Embed IPython in your programs
151
 
------------------------------
152
 
 
153
 
A few lines of code are enough to load a complete IPython inside your own
154
 
programs, giving you the ability to work with your data interactively after
155
 
automatic processing has been completed. See :ref:`here <embedding>` for more.
156
 
 
157
 
Use the Python profiler
158
 
-----------------------
159
 
 
160
 
When dealing with performance issues, the %run command with a -p option
161
 
allows you to run complete programs under the control of the Python profiler.
162
 
The %prun command does a similar job for single Python expressions (like
163
 
function calls).
164
 
 
165
 
Use IPython to present interactive demos
166
 
----------------------------------------
167
 
 
168
 
Use the IPython.demo.Demo class to load any Python script as an interactive
169
 
demo. With a minimal amount of simple markup, you can control the execution of
170
 
the script, stopping as needed. See :ref:`here <interactive_demos>` for more.
171
 
 
172
 
Run doctests
173
 
------------
174
 
 
175
 
Run your doctests from within IPython for development and debugging. The
176
 
special %doctest_mode command toggles a mode where the prompt, output and
177
 
exceptions display matches as closely as possible that of the default Python
178
 
interpreter. In addition, this mode allows you to directly paste in code that
179
 
contains leading '>>>' prompts, even if they have extra leading whitespace
180
 
(as is common in doctest files). This combined with the '%history -tn' call
181
 
to see your translated history (with these extra prompts removed and no line
182
 
numbers) allows for an easy doctest workflow, where you can go from doctest
183
 
to interactive execution to pasting into valid Python code as needed.
184
 
 
185
 
Source code handling tips
186
 
=========================
187
 
 
188
 
IPython is a line-oriented program, without full control of the
189
 
terminal. Therefore, it doesn't support true multiline editing. However,
190
 
it has a number of useful tools to help you in dealing effectively with
191
 
more complex editing.
192
 
 
193
 
The %edit command gives a reasonable approximation of multiline editing,
194
 
by invoking your favorite editor on the spot. IPython will execute the
195
 
code you type in there as if it were typed interactively. Type %edit?
196
 
for the full details on the edit command.
197
 
 
198
 
If you have typed various commands during a session, which you'd like to
199
 
reuse, IPython provides you with a number of tools. Start by using %hist
200
 
to see your input history, so you can see the line numbers of all input.
201
 
Let us say that you'd like to reuse lines 10 through 20, plus lines 24
202
 
and 28. All the commands below can operate on these with the syntax::
203
 
 
204
 
    %command 10-20 24 28 
205
 
 
206
 
where the command given can be:
207
 
 
208
 
    * %macro <macroname>: this stores the lines into a variable which,
209
 
      when called at the prompt, re-executes the input. Macros can be
210
 
      edited later using '%edit macroname', and they can be stored
211
 
      persistently across sessions with '%store macroname' (the storage
212
 
      system is per-profile). The combination of quick macros,
213
 
      persistent storage and editing, allows you to easily refine
214
 
      quick-and-dirty interactive input into permanent utilities, always
215
 
      available both in IPython and as files for general reuse.
216
 
    * %edit: this will open a text editor with those lines pre-loaded
217
 
      for further modification. It will then execute the resulting
218
 
      file's contents as if you had typed it at the prompt.
219
 
    * %save <filename>: this saves the lines directly to a named file on
220
 
      disk.
221
 
 
222
 
While %macro saves input lines into memory for interactive re-execution,
223
 
sometimes you'd like to save your input directly to a file. The %save
224
 
magic does this: its input sytnax is the same as %macro, but it saves
225
 
your input directly to a Python file. Note that the %logstart command
226
 
also saves input, but it logs all input to disk (though you can
227
 
temporarily suspend it and reactivate it with %logoff/%logon); %save
228
 
allows you to select which lines of input you need to save.
229
 
 
230
 
 
231
 
Lightweight 'version control'
232
 
=============================
233
 
 
234
 
When you call %edit with no arguments, IPython opens an empty editor
235
 
with a temporary file, and it returns the contents of your editing
236
 
session as a string variable. Thanks to IPython's output caching
237
 
mechanism, this is automatically stored::
238
 
 
239
 
    In [1]: %edit
240
 
 
241
 
    IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py
242
 
 
243
 
    Editing... done. Executing edited code...
244
 
 
245
 
    hello - this is a temporary file
246
 
 
247
 
    Out[1]: "print 'hello - this is a temporary file'\n"
248
 
 
249
 
Now, if you call '%edit -p', IPython tries to open an editor with the
250
 
same data as the last time you used %edit. So if you haven't used %edit
251
 
in the meantime, this same contents will reopen; however, it will be
252
 
done in a new file. This means that if you make changes and you later
253
 
want to find an old version, you can always retrieve it by using its
254
 
output number, via '%edit _NN', where NN is the number of the output
255
 
prompt.
256
 
 
257
 
Continuing with the example above, this should illustrate this idea::
258
 
 
259
 
    In [2]: edit -p
260
 
 
261
 
    IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py
262
 
 
263
 
    Editing... done. Executing edited code...
264
 
 
265
 
    hello - now I made some changes
266
 
 
267
 
    Out[2]: "print 'hello - now I made some changes'\n"
268
 
 
269
 
    In [3]: edit _1
270
 
 
271
 
    IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py
272
 
 
273
 
    Editing... done. Executing edited code...
274
 
 
275
 
    hello - this is a temporary file
276
 
 
277
 
    IPython version control at work :)
278
 
 
279
 
    Out[3]: "print 'hello - this is a temporary file'\nprint 'IPython version control at work :)'\n"
280
 
 
281
 
 
282
 
This section was written after a contribution by Alexander Belchenko on
283
 
the IPython user list.
284
 
 
285
 
 
286
 
Effective logging
287
 
=================
288
 
 
289
 
A very useful suggestion sent in by Robert Kern follows:
290
 
 
291
 
I recently happened on a nifty way to keep tidy per-project log files. I
292
 
made a profile for my project (which is called "parkfield")::
293
 
 
294
 
    include ipythonrc
295
 
 
296
 
    # cancel earlier logfile invocation:
297
 
 
298
 
    logfile ''
299
 
 
300
 
    execute import time
301
 
 
302
 
    execute __cmd = '/Users/kern/research/logfiles/parkfield-%s.log rotate'
303
 
 
304
 
    execute __IP.magic_logstart(__cmd % time.strftime('%Y-%m-%d'))
305
 
 
306
 
I also added a shell alias for convenience::
307
 
 
308
 
    alias parkfield="ipython -pylab -profile parkfield" 
309
 
 
310
 
Now I have a nice little directory with everything I ever type in,
311
 
organized by project and date.
312
 
 
313
 
 
314
 
Logging to a file
315
 
=================
316
 
 
317
 
Here is an alternative logging solution that lets you record your sessions in
318
 
a daily time-stamped log-files.
319
 
 
320
 
Add the following lines or make it a-like to your ipy_user_conf.py::
321
 
 
322
 
    from time import strftime
323
 
 
324
 
    def main():   
325
 
 
326
 
        try:
327
 
            ldir = '/home/$YOUR_USERNAME_HERE/.ipython/'
328
 
            filename = os.path.join(ldir, strftime('%Y-%m-%d')+".py")
329
 
            notnew = os.path.exists(filename)
330
 
            ip.IP.logger.logstart(logfname=filename, logmode='append')
331
 
            log_write = ip.IP.logger.log_write
332
 
            if notnew:
333
 
                log_write("# =================================")
334
 
            else:
335
 
                log_write("#!/usr/bin/env python \n# %s.py \n"
336
 
                          "# IPython automatic logging file" %
337
 
                          strftime('%Y-%m-%d'))
338
 
            log_write("# %s \n# =================================" %
339
 
                      strftime('%H:%M')) 
340
 
            print " Logging to "+filename
341
 
 
342
 
        except RuntimeError:
343
 
            print " Already logging to "+ip.IP.logger.logfname
344
 
 
345
 
 
346
 
Contribute your own: If you have your own favorite tip on using IPython
347
 
efficiently for a certain task (especially things which can't be done in
348
 
the normal Python interpreter), don't hesitate to send it!
 
131
 
 
132
Configuration
 
133
=============
 
134
 
 
135
Much of IPython can be tweaked through configuration. To get started, use the
 
136
command ``ipython profile create`` to produce the default config files. These
 
137
will be placed in :file:`~/.ipython/profile_default` or
 
138
:file:`~/.config/ipython/profile_default`, and contain comments explaining what
 
139
the various options do.
 
140
 
 
141
Profiles allow you to use IPython for different tasks, keeping separate config
 
142
files and history for each one. More details in :ref:`the profiles section
 
143
<profiles>`.
 
144
 
 
145