Next: Command-line use
Up: IPython An enhanced Interactive
Previous: Initial configuration of your
Quick tips
IPython can be used as an improved replacement for the Python prompt, and for that
you don't really need to read any more of this manual. But in this section we'll
try to summarize a few tips on how to make the most effective use of it for everyday
Python development, highlighting things you might miss in the rest of the manual
(which is getting long). We'll give references to parts in the manual which provide
more detail when appropriate.
- The TAB key. TAB-completion, especially for attributes, is a convenient way to
explore the structure of any object you're dealing with. Simply type object_name.<TAB>
and a list of the object's attributes will be printed (see sec. 6.4
for more). Tab completion also works on file and directory names, which combined
with IPython's alias system allows you to do from within IPython many of the things
you normally would need the system shell for. Note that this feature does not work
on platforms lacking readline support, such as Windows.
- Explore your objects. Typing object_name? will print all sorts of details
about any object, including docstrings, function definition lines (for call arguments)
and constructor details for classes. The magic commands @pdoc, @pdef,
@psource and @pfile will respectively print the docstring, function
definition line, full source code and the complete file for any object (when they
can be found). If automagic is on (it is by default), you don't need to type the
'@' explicitly. See sec. 6.3 for more.
- The @run magic command allows you to run any python script and load all
of its data directly into the interactive namespace. Since the file is re-read
from disk each time, changes you make to it are reflected immediately (in contrast
to the behavior of import). I rarely use import for code I am
testing, relying on @run instead. See sec. 6.1 for more on
this and other magic commands, or type the name of any magic command and ? to get
details on it. See also sec. 6.8 for a recursive reload command.
- Use the Python debugger, pdb3. The @pdb command allows you to toggle on and off the automatic invocation
of the pdb debugger at any uncaught exception. The advantage of this is that pdb
starts inside the function where the exception occurred, with all data still
available. You can print variables, see code, execute statements and even walk
up and down the call stack to track down the true source of the problem (which
often is many layers in the stack above where the exception gets triggered).
Running programs with @run and pdb active can be an efficient to develop
and debug code, in many cases eliminating the need for print statements
or external debugging tools. I often simply put a 1/0 in a place where
I want to take a look so that pdb gets called, quickly view whatever variables
I need to or test various pieces of code and then remove the 1/0.
- Use the output cache. All output results are automatically stored in a global dictionary
named Out and variables named _1, _2, etc. alias them.
For example, the result of input line 4 is available either as Out[4]
or as _4. Additionally, three variables named _, __
and ___ are always kept updated with the for the last three results.
This allows you to recall any previous result and further use it for new calculations.
See sec. 6.11 for more.
- Put a ';' at the end of a line to supress the printing of output. This
is useful when doing calculations which generate long output you are not interested
in seeing. The _* variables ant the Out[] list do get
updated with the contents of the output, even if it is not printed. You can thus
still access the generated results this way for further processing.
- A similar system exists for caching input. All input is stored in a global list
called In , so you can re-execute lines 22 through 28 plus line 34 by
typing 'exec In[22:29]+In[34]' (using Python slicing notation).
If you need to execute the same set of lines often, you can assign them to a macro
with the @macro function. See sec. 6.10 for
more.
- Use your input history. The @hist command can show you all previous input,
without line numbers if desired (option -n) so you can directly copy and
paste code either back in IPython or in a text editor. You can also save all your
history by turning on logging via @logstart; these logs can later be either
reloaded as IPython sessions or used as code for your programs.
- Define your own macros with @macro. This can be useful for automating
sequences of expressions when working interactively.
- Define your own system aliases. Even though IPython gives you access to your system
shell via the ! prefix, it is convenient to have aliases to the system commands
you use most often. This allows you to work seamlessly from inside IPython with
the same commands you are used to in your system shell.
IPython comes with some pre-defined aliases and a complete system for changing
directories, both via a stack (see @pushd, @popd and @ds)
and via direct @cd. The latter keeps a history of visited directories
and allows you to go to any previously visited one.
- Use profiles to maintain different configurations (modules to load, function definitions,
option settings) for particular tasks. You can then have customized versions of
IPython for specific purposes. See sec. 7.2 for more.
- Embed IPython in your programs. A few lines of code are enough to load a complete
IPython inside your own programs, giving you the ability to work with your data
interactively after automatic processing has been completed. See sec. 9
for more.
- Use the Python profiler. When dealing with performance issues, the @run
command with a -p option allows you to run complete programs under the
control of the Python profiler. The @prun command does a similar job for
single Python expressions (like function calls).
If you have your own favorite tip on using IPython efficiently for a certain task
(especially things which can't be done in the normal Python interpreter), don't
hesitate to send it!
Next: Command-line use
Up: IPython An enhanced Interactive
Previous: Initial configuration of your
Fernando Perez
2003-08-25