| Author: | Jeff Rush |
|---|---|
| Copyright: | 2007 Tau Productions Inc. |
| License: | Creative Commons Attribution-ShareAlike 3.0 |
| Date: | June 26, 2007 |
| Version: | 1 |
| Series: | 5-Minutes with Python |
A 5-minute introduction to the enhanced interactive shell 'IPython'. Superior to the default Python shell, IPython provides power object introspection features, easy access to operating system commands and text editor integration.
Hello, my name is Jeff Rush and this talk is part of the "5-Minutes with Python" series. In this brief talk I'll provide a comprehensive survey of the capabilities of the enhanced interactive shell known as "IPython".
If it looks like something you might want to investigate further, there is a follow-up talk entitled "A Demonstration of the 'IPython' Interactive Shell" that gets more into the details using an interactive demonstration.
Table of Contents
In this talk I'll define what IPython is, why you might want to use
it, and generally
what mechanisms it adds to the interactive
prompt.
I'll cover the introspection capabilities it gives you,
how it provides seamless access to the commands of your system
shell, and
integrates with your choice of text editors, allowing
you to work in an iterative fashion.
And I'll talk about how IPython can be hooked into your own
programs, the execution profiling it offers and how you can add extra
information to your tracebacks.
I'll conclude with where to get IPython and what resources are
available in that community.
an alternative to the default shell
non-GUI in nature
cross-platform and free
- invocable from within programs
- explicitly for code examination
- implicitly upon uncaught exceptions
IPython is an alternative to the default Python shell,
increasing your effectiveness at the interactive prompt while retaining its
familiarity and keyboard friendliness.
Like the default shell, it is a text-based, non-GUI method of
interaction. However it does have good support to run side-by-side with
various 2-d and 3-d data plotting libraries and GUI toolkits.
IPython runs on all the popular operating systems and it is free
software so you can use it everywhere.
It can be easily invoked from within your programs, either
implicitly upon occurrence of an uncaught exception, or
explicitly to put you into an interactive environment with the
variable namespace current at that point.
This makes IPython useful for both debugging and for those blended types of programs with heavy computation intermixed with exploration of the results, as in scientific data visualization.
- object introspection on steroids
- access to the system command-shell
- extensible into a domain-specific command language
- can use your own text editor
IPython gives you the ability to powerfully introspect an object,
showing its docstring, call signature, online help pages or source code.
You can enter system commands, change directories and edit source
files, while remaining within IPython. This is especially nice for those
platforms that lack a good command-line shell.
IPython has what are called "magic" commands, which provide
high-level functions that control the behavior of IPython itself. You can
define your own such commands and in combination with an input-filtering
capability of IPython, construct a command language specific to the problem
domain at hand.
IPython is friendly to those of us who are particular to our text
editors, moving source seamlessly back and forth between the editor and
IPython while not forcing the use of a specific IDE text editor.
caches the last N commands and their output
view, log, save, replay and edit this history
has a concept of namespaces
- interprets names, by precedence
- variables
- magic commands
- shell command aliases
At the prompt, IPython caches your most recent inputs, along with
the output they generated.
This command history is easily viewed, logged to a file and
selectively saved into source files. You can bring portions into your
editor and have changes reevaluated into your session. And you can quickly
convert groups of frequently used commands into named macros.
IPython, like the default Python shell, gives you access to the
interactive namespace, for manipulating variables. However it also
provides easy ways to filter and display groups of variables, and to reset
the namespace to its startup state.
Because IPython accepts magic and system commands as well as source
code, IPython uses rules to decide how to interpret a name when entered.
If there is a variable with that name, it displays its value.
Otherwise it consults a registry of "magic commands", essentially
special names that invoke functions that manipulate your IPython session,
navigate directories and manage files.
And last it looks up the name in a list of aliases that map to
commands issued to the underlying system shell.
input history and namespaces are searchable
- tab completion
- keywords, methods, variables, files
get brief and full info on any object
IPython gives you a number of ways to quickly look up information, almost eliminating the need to keep reference manuals at hand.
The input history and variable namespaces are fully searchable by
wildcard and type of object.
IPython provides extensive tab-completion features,
expanding names into variables, directories and filenames. It understands
nested namespaces in that you can type partial expressions like "classname
dot <TAB>" and have it display the methods and attributes available to you.
And you can query an object for information about its type, the
namespace in which it is located, the source file from which it was loaded,
and its docstring. You can also display the source fragment that defined
it, and its call signature if the object is callable.
- ! escape mechanism
- aliases relate text to commands
- command output into vars
- var substitution into commands
- directory navigation
- background threading
Arbitrary system commands can be entered by prefixing them with the
exclamation or bang symbol, as a common shell escape mechanism.
Commands, perhaps with default arguments, can be aliased to a
simple name, for easier use.
Command output, optionally split on linebreaks or spaces, can be
collected into Python variables,
and in turn, variables can be substituted into commands.
The system shell mechanism for IPython comes with a rich set of
directory navigation commands, with bookmarking, history and a pushdown
stack of visited places. This makes working with complex frameworks across
directory trees much easier.
And you can cause arbitrary Python expressions to be executed in
background threads and, with the extended version of IPython called
IPython1, distributed across multiple CPU cores or host machines.
line-oriented, not multiline
- magic command: %edit
- of files, objects, lines of input
automatic rentry of editor upon syntax error
simple source versioning
IPython is
line-oriented -- while you can scroll back, change and
rerun previous inputs, that would be awkward for definitions of functions
or classes.
IPython provides a magic "edit" command, that invokes your
text editor on various kinds of targets. Upon saving and exiting from your
editor, the source is reevaluated into the current interactive namespace.
You can pass to "edit" the name of a file you wish to change.
But you can also pass an object reference. IPython will look up
the source file from which it came, invoke your editor on that file,
and position you at the the definition. Very cool.
And you can pass the line numbers of previous inputs, as a range or
list, and their text will loaded together into your editor. Upon exit, each
line will be re-evaluated into the current namespace.
We've all made a quick change in our editor, only to find out at
runtime that we've made a trivial syntax error. IPython, upon reloading
your source after an edit, can detect that syntax error and automatically
take you back into the editor at the point it occurred.
IPython provides a lightweight form of version control over edits.
By retaining an historical copy of the result of each command, including
edits, you can pass in a input line number representing a previous state,
letting you revise and reload from that point. Quite useful when teaching
or demonstrating.
- can be invoked from within your programs
- uncaught exceptions enter the debugger
- measure execution time of code fragments
- integration with Python profiler
- extended tracebacks with color
IPython provides ways to instrument your source with useful diagnostics.
You can put calls in your program to start up an interactive
session, giving you access to the variable namespace at that point.
You can set IPython to enter the Python debugger upon the
occurrence of an uncaught exception, letting you walk the stack frame and
query variables.
You can measure the CPU or wallclock execution time of code
fragments, or
run your code under the Python profiler for more
detailed measurements.
And you can set your preferred display of Python tracebacks,
colorized for readability and with an extended mode that shows more
information.
- http://ipython.scipy.org/moin/
- users and developers mailing lists
- online documentation
- bug and feature request tracking system
- articles, videos and presentations
- Contact:
Jeff Rush <jeff@taupro.com>
IPython is a mature, exciting project with a strong community.
It has active mailing lists, separate for users and maintainers.
A complete manual is available for browsing online or as a PDF
document.
And it has a tracking system for bug reports and feature requests.
The project website provides magazine articles, video tutorials and
slide presentations.
I recommend you check it out. There are other aspects of IPython I have not covered such as usage tips and techniques along with contributed extensions and profiles.
Thanks for listening and look for the longer talk that
demonstrates the actual use of IPython.
These slides and script are available under the Creative Commons Attribute-ShareAlike license, for use in your own presentations.