An Introduction to IPython

Hopefully some of you will end up loving this tool as much as I do. I think I originally saw it on a defcon talk, and never looked back. Perhaps it’s a bit long-winded, but I wanted to be detailed. Personally, I’ve used it almost everyday since learning to use it, it’s become an invaluable tool for working with Python. I urge anyone who loves their Python to give it a go.

What Is IPython?

In a nutshell, it’s the IDLE* you always dreamed of. IPython is a richly featured replacement for the standard Python IDLE. It offers a much wider range of functionality, that the standard IDLE, which generally ships with python, sorely lacks. It’s a great tool for learning Python faster than you would without it, kind of like a handy cheat-sheet that’s at your fingertips. It excels as a quick Python object reference (examples below), for testing snippets of code on-the-fly and exploring new modules. But these are just a few of the many features useful to the Python scripter. In this introduction, I will show off some it’s most useful features. IPython is available for Linux, Mac and Windows.

* Integrated Development & Learning Environment

Install IPython

I will assume here that you are using Kali linux/debian. If not, just replace the commands with the relevant ones for your distro. To install on kali, type:

apt-get install ipython

OR for the python 3.x version

apt-get install ipython3

If you’re trying this on windows, already have Python installed(I will assume you do), and have some experience installing packages, you can get it with the command:

pip install ipython replace with ipython3 if you use python 3.x

Here are some links that can help with installation issues:


and here:
http://ipython.org/install.html .

Start IPython

To start IPython, open a command prompt, and type ipython
You should see this new prompt in the terminal:

In my examples below, I will be using Kali and the IPython for Python 2.x

Why Use IPython?

Exploring objects, a handy on-the-fly reference.

IPython makes exploring and learning more about python objects (modules, data-types, data-structures etc etc.) a breeze. See for yourself. Let’s say you wanted to find out more about the itertools module (a very useful module). IPython makes this simple:



The command takes the form object_name?
Now hit <return>, and Boom!





We get the modules docstring, and we find out that itertools contains “Functional tools for creating and using iterators”… Great!

Pressing <Enter> or the up/down directional keys will navigate through the rest of the docstring. We can escape back to IPython by pressing ‘q’.

However, it’s not just modules you can do this with, you can also do this for pythons types (str, list, tuple, dict … etc), variables you have created and … well pretty much any object in python… and that’s pretty useful, because in python, everything is an object!




**Tab Completion**

Another great feature of IPython is it’s Tab-completion. We can find out what methods itertools has by typing:

object_name. <TAB> ( <TAB> here means press the TAB key)



Now I want to know more about itertools.permutations, again I can use object_name?


This is also useful if you can't quite remember the name of the method you want, but you do remember for instance, the letter it started with. In this instance we could type,

object_name.first_letter <TAB>

(remember this means hit tab) which gives you all the methods starting with that letter.



In short, typing object_name. <TAB> will work for almost any object, displaying it’s attributes and methods, if it has any. This also works on file and directory names and any objects you may have created yourself!


Magic Functions

These are ‘magic’ predefined functions whose syntax is much like that of a command line call. There are two types, line magics and cell magics.

Line magics syntax takes the form

%func_name <args>

where <args> is the rest of the line, without quotes or parentheses.

Cell magics syntax is
%%func_name <args>
<args>
<args>

where <args> are both the rest of the line, and in a separate argument, the lines that follow it. Note that we use % twice for cell magics, once for line magics.

A brief example:




A full list of magic functions can be seen by typing %lsmagic

Finally, to exit IPython, simply type exit and hit <Enter>

In Conclusion

IPython is a very useful tool for scripting with python as a quick, easy, in-depth reference, scratchpad (for testing snippets of code on the the fly) and last, but not least, for expanding your knowledge of python (very handy for exploring new modules). I hope this will prove useful, it’s my first post, and I appreciate any comments or criticism. Thanks for reading!

References:

5 Likes

Thanks for the tut! It was extremely useful! I don’t really have any criticism for this post, although to be frank I love how you clearly explained what was happening for each step. It makes it much easier for people who don’t know what you’re introducing, as well as helping them down the right path in terms of how it should be done.

1 Like

This is basically irb (Interactive Ruby) + man, but for Python! Cool!

1 Like

Say oaktree, people keep telling me that Ruby is a “no-go” in terms of scripting and I should just try to stay with Python. Of course I’m awfully hesitant about doing that due to the fact that Ruby is also superior in many ways as well. What say you?

Thanks for the heads up.
I can’t bother with any fancy IDE because i am mostly working via SSH, and IPython seems like a good alternative to the standard Python interpreter for making programming easier.

1 Like

For anyone who is in the market for one, I use PyCharm by Jetbrains as my Python IDE and would highly recommend it. People complain it’s a bit slow, since it’s coded in Python apparently, but I don’t mind waiting a few extra seconds for it to start up. Free full licenses for students on most of their products too, but the free versions are quite fully featured.

This topic was automatically closed after 30 days. New replies are no longer allowed.