DEV Community

Cover image for IPython at Lightspeed ⚡
Ali Sherief
Ali Sherief

Posted on

IPython at Lightspeed ⚡

Just a heads up, the audio series is not finished yet. I tweeted that I'm writing under stressful people conditions a few days ago:

Liquid error: internal

(In fact I even missed my usual schedule for publishing on Mondays.)

In an effort to keep the audio series polished, this week I'm blogging about something else to give me more time to organize part 3. This post will be relatively short compared to most of my other articles which are usually 8 minutes long.

So with that out of the way, I did talk about Jupyter in my last post. I thought I should elaborate on it some more.

Jupyter and IPython are closely related, jupyter being the name of an umbrella of projects while IPython is the interactive shell itself. IPython can function as a REPL for other languages besides python, particularly for Ruby and PHP which do not ship with proper REPLs that I know of.

A REPL is very important for any language to have because it allows you to run individual code snippets without having to create entire files to run your script. They are especially crucial when trying to learn the language, in which case you may not even know how to create a proper file yet.

I will cover IPython in this post and leave Jupyter QtConsole and Jupyter Notebook for another post.

Installation

IPython can be installed by itself by installing the ipython package.

IPython

Once you get used to this shell, you probably won't want to use the normal Python interpreter again. It has a lot of features that make it superior to the python shell.

All modern IPython versions only support Python >= 3.4. In particular, the last version to support Python 2.7 was IPython 5.x.

This is what a typical IPython session looks like (too lazy to take my own screenshots right now):

IPython console

BEGIN quick reference

The only command I want you to memorize before leaving thie page is %quickref. It shows you all the commands available.

END quick reference

IPython can fetch documentation for any imported object using the ? command, so the following:

In [1]: import math

In [2]: ?math

In [3]: ?math.sqrt
Enter fullscreen mode Exit fullscreen mode

will display documentation for the math module and the sqrt() function.

The ?? command will display documentation and the source code for the function provided it was written in python. In particular, the builtin functions of Python are C modules so that code cannot be looked up with this command.

Pressing Ctrl-D will kindly ask you whether you want to exit the interpreter like: Do you really want to exit ([y]/n)?. The exit command ends the IPython session immediately.

! will run a shell command, and can be assigned to a variable using variable = !shell_command. It saves you from having to suspend the interpreter with Ctrl-Z. Here are some examples:

In [1]: v = !date 

In [2]: v
Out[2]: ['Thu Feb  6 20:33:25 CAT 2020']

In [3]: v = !ls

In [4]: v
Out[4]: ['audio', 'http', 'pytuning-devel', 'zignal-devel']
Enter fullscreen mode Exit fullscreen mode

One of the most useful features of IPython is the ability to quickly change directory without having to import os. The %cd target_directory command changes directories and accepts any well formed path (Unix at least; I don't have Windows handy to test on but partition letters should also work). You can even bookmark arbitrary folders and change the working directory to one of those bookmarks. The documentation for %bookmark explains it's usage so concisely that I'm pasting it here:

In [1]: ?%bookmark                                                             
Docstring:
Manage IPython's bookmark system.

%bookmark <name>       - set bookmark to current dir
%bookmark <name> <dir> - set bookmark to <dir>
%bookmark -l           - list all bookmarks
%bookmark -d <name>    - remove bookmark
%bookmark -r           - remove all bookmarks

You can later on access a bookmarked folder with::

  %cd -b <name>

or simply '%cd <name>' if there is no directory called <name> AND
there is such a bookmark defined.

Your bookmarks persist through IPython sessions, but they are
associated with each profile.
Enter fullscreen mode Exit fullscreen mode

You might have also noticed that ? can also get documentation for these metacommands. That's not what they're actually called but I refer to it as that.

%store lets you persist python variables. It's used like this:

In [1]: l = ['hello',10,'world']
In [2]: %store l
In [3]: exit

(IPython session is closed and started again...)

ali@localhost:~$ ipython
In [1]: l
NameError: name 'l' is not defined
In [2]: %store -r
In [3]: l
Out[3]: ['hello', 10, 'world']
Enter fullscreen mode Exit fullscreen mode

IPython is smart and usually inserts a new line when you try to continue the code in the console. You can also erase the continuation line by pressing backspace.

If your code makes an exception, you can type %debug to spawn a pdb session for you and hone into the line that made the error. You can also toggle whether to spawn a pdb automatically when an exception is raised by running %pdb on or %pdb off.

The _, __ and ___ (single, double and triple underscores) run the previous, second-to-previous and third-to-previous command respectively. The %history command displays the most recent python commands you entered:

In [16]: %history                                                               
int?
?int
??int
%quickref
%paste
%debug
raise RuntimeError("help!")
%debug
%pdb
raise RuntimeError("help!")
%pdb
%pdb off
%pdb on
%pdb off
?%history
%history

In [17]:    
Enter fullscreen mode Exit fullscreen mode

The %edit command gives you the functionality of an editor in a terminal. This will launch your default editor, or vi/notepad if no default editor is set, and you can type all the commands that you wan to run in this session. All variables made in the file will be available in the interpreter session. The file will be run after you save and quit the editor. You can also discard your edits and quit the editor but if you do that, the unedited file will still be ran in the session anyway, and if the file is empty it won't be run at all.

%edit makes a temporary file while %edit path-to-file will edit whatever file you pass.

The last command I want to cover is %load path-to-file and we saw that command being used in the last article to load the PyTuning functions. It takes a file and it runs it in the session, so you don't have to %edit file and immediately quit just to run it.

And we're done

Let me know if you see anything incorrect in the article. Stay tuned for part 3 of the audio series ⚡

Image by beate bachmann from Pixabay

Top comments (0)