Ipython
Updated:
IPython
Documentation found here.
IPython is an interactive python shell
Features
IPython has the following features that might be useful:
- history, because who has time to type the same stuff all the time
- output logging
- syntax highlighting
- multi-line editing
- command-line editor
- access to system commands
- persisting python variables across sessions
- input/output caching
- auto-parenthesis
- timing execution of statements
Input/Output Caching
Input
Past inputs to the ipython shell can be recalled by pressing the up key or by making use of the
input caching system. _i
, _ii
, and _iii
store the last three previous inputs, respectively.
_i<n>
refers to the input number n
, as shown on the ipython line prompt.
This lets you use previous inputs and manipulate them in python as strings.
%history
will print a range of these _i
variables.
%history -g <grep string>
will allow for grepping through history.
%rerun _i
will rerun the previous input commands.
%recall_i
will edit previous input commands.
Output
A similar system exists for output cache.
_
, __
, and ___
correspond the last three previous outputs.
_<n>
refers to the output number n
, as shown on the output prompt.
Memory Demands
This caching system can increase memory demands, since the outputs are not automatically garbage collected.
InteractiveShell.cache_size
can be configured to limit the size of the cache.
Setting this to 0 means disabling caching.
%reset
and %del
can be used to clear out objects from memory.
Automatic Parentheses
Callable objects can be invoked without parentheses:
foo(1, 2, 3)
foo 1, 2, 3
This requires enabling via %autocall
Pasting code from Python or IPython
IPython is smart enough to recognize interactive output from python or ipython shells. You can copy and paste from a previous interactive session and it’ll just work™.
Suppressing Output
If an input is terminated with a semi-colon, this will suppress output and keep the output out of output cache. This useful when you want to run a command but don’t care about the output.
Editing
The magic %edit
will allow for editing with an external editor.
This creates a temporary file and pastes the output when the editor is closed.
You can re-edit existing lines by passing the input cache line number.
ptpython
ptpython is a wrapper on top of ipython that adds readline autocomplete (suggestions). Auto suggesting completions is great as it addresses discoverability, one of common weaknesses of terminal tools.
Magic Commands
Magic | Description
—
%edit
| Calls the EDITOR to edit multi-line inputs.
%quickref
| Displays a cheatsheet
%time
| Captures execution timings, like the unix time
command.
%timeit
| More powerful than %time
, it uses the timeit
python module
%whos
| Prints all interactive variables and their values
IPython Configs
An IPython config file can be created at ~/.ipython/profile_default/ipython_config.py
.
This file is ran before ipython startup and can be used to set default config settings,
automatically execute ipython lines such as importing libraries or default test data.
An example can be found in the docs.