Although your Python shell is useful, it’s lacking quite a lot of magic you can discover by using IPython. IPython is an enhanced Python shell that offers you some easy-to-use shortcuts and extra power when working with Python in a shell environment. It was originally developed by scientists and students who wanted an easier shell for their Python use. It has since become the de facto standard for learning and interacting with Python via an interpreter.
IPython gives you quite a lot of functionality lacking in the standard Python shell. The benefits of installing and using IPython as your shell are numerous. Its features include:
Easy-to-read documentation hooks
Autocompletion and magic commands for library, class, and object exploration
Inline image and chart generation
Helpful tools to view history, create files, debug your script, reload your script, and more
Built-in shell command usage
Auto-imports on startup
It’s also one of the core components of Jupyter, a shared notebook server allowing for rapid-cycle data exploration in a browser. We covered using Jupyter for code sharing and presentation in Chapter 10.
IPython is easy to install with pip:
pip install ipython
If you are using more than one virtual environment, you might want to install IPython globally or within each virtual environment. To begin using IPython, simply type ipython in your terminal window. You should see a prompt similar to this:
$ ipython Python 2.7.6 (default, Mar 22 2014, 22:59:56) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.
In [1]:
You can now type Python commands as you would in a normal Python shell. For example:
In[1]:1+1Out[1]:2In[2]:fromdatetimeimportdatetimeIn[3]:datetime.now()Out[3]:datetime.datetime(2015,9,13,11,47,49,191842)
When you need to exit the shell, you can type quit(), exit(), or Ctrl-D on Windows/Linux or Cmd-D on Mac.
IPython has numerous so-called magic functions to help you as you explore and program. Here are some of the most useful ones, especially for beginning developers.
To easily see everything you have imported and all active objects, you can type %whos or %who. Let’s take a look at their usage:
In[1]:foo=1+4In[2]:bar=[1,2,4,6]In[3]:fromdatetimeimportdatetimeIn[4]:baz=datetime.now()In[5]:%whobarbazdatetimefooIn[6]:%whosVariableTypeData/Info--------------------------------barlistn=4bazdatetime2015-09-1311:53:29.282405datetimetype<type'datetime.datetime'>fooint5
This can be incredibly helpful if you have forgotten a variable name or want to see what you have stored in your variables in one concise list.
Another useful tool is the ability to quickly look up documentation related to libraries, classes, or objects. If you type a ? at the end of the name of the method, class, library, or attribute, IPython will attempt to retrieve any related documentation and display it inline. For example:
In [7]: datetime.today?Type: builtin_function_or_methodString Form:<built-in method today of type object at 0x7f95674e0a00>Docstring: Current date or datetime:same as self.__class__.fromtimestamp(time.time()).
There are tons of IPython extensions and functions similar to these that are tremendously useful for development, particularly as you grow as a developer and encounter more complicated issues. Table F-1 lists some of the most useful ones, but there are also some great presentations and conference talks and interactive examples available online, as well as the library’s well-written documentation.
All IPython extensions must be loaded using %load_ext extension_name at the beginning of your IPython session. If you’d like to install extra extensions, there’s a great list of available extension and their uses on GitHub.
| Command | Description | Purpose | Documentation |
|---|---|---|---|
|
Extension allowing you to reload all imported scripts with one call |
Great for active development, when you are changing a script in your editor and debugging it in your IPython shell |
http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html |
|
Extension allowing you to store saved variables for use in a later session |
Best for use if you need to save some variables you will always need or if you are interrupted and need to save your current work for later use |
http://ipython.org/ipython-doc/dev/config/extensions/storemagic.html |
|
Prints your session history |
Shows an output of what you’ve already run |
https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-history |
|
Debugging module for interactive debugging with longer calls |
Powerful debugging library, especially useful when importing longer scripts or modules |
https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-pdb |
|
Imports |
Allows you to use statistics and charting functionality within your IPython shell |
https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-pylab |
|
Saves your session history to an output file |
An easy way to start writing a script if you’ve spent a long time debugging |
https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-save |
|
Times the execution of one or more lines of code |
Handy for performance-tuning your Python scripts and functions |
https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-timeit |
There are many more magic commands available. Their usefulness will depend on what you use IPython for in your development, but employing them as you grow as a developer will likely shed light on other tasks IPython can simplify for you.
Whether you use IPython only in a notebook or in your active terminal development, we believe it will help you write and understand Python and grow as a developer. Much of your early development will be exploring how Python works and what errors and exceptions you encounter along the way. IPython is great for these lessons, as you can try again on the next input line. We hope IPython will keep you learning and writing Python for many years to come.