Python and IPython

      No Comments on Python and IPython

I recently came across IPython while reading some Python development blogs.  IPython is an alternative to the standard Python shell that offers some additional features.  When I first read about IPython, I was a little confused because many people refer to it as the ‘Python interactive shell’.  While IPython is an interactive shell, it is not the Python interactive shell.  For instance, we can enter the Python interactive shell just by typing ‘python’ on our Python development box…

image
So, what we really did here was invoke the Python interpreter in interactive mode.  In this mode, commands can be read from the TTY and directly interpreted.  So for example, we can do something like this…

image
The Python code we type is directly interpreted and we get the output we would expect.  So instead of using the Python interpreter to run a .py script, we could do it all directly from the interpreter.  So the example from our Python up and running post works just as well in interactive mode as it did when run as a script…

image
So that’s Python interactive mode.  Now, let’s talk about IPython.  The first thing we need to do is install IPython.  We can actually do this with PIP! 

pip install ipython

Then to run it we just type ‘ipython’ instead of ‘python’…

image
So I think this sort of nicely sums up what IPython is.  It’s an enhanced version of interactive Python.  So what are the enhancements?  The big one for me is tab completion.  If I start typing my ‘hello world’ script into IPython, I can tab complete the commands…

image
So here I just hit tab and IPython pulls all the available modules (and other stuff) from the package pyfiglet.  This is pretty darn helpful if you’re testing out code or just playing around with modules.  It’s also super helpful if we want to look at an object in Python code.  For instance, if we continue our example the next line creates a object called’ ‘f’ of type Figlet.  After we create the object, we can tell what type it is by typing the object name followed by a question mark…

image 
Here we can get all kinds of info about the object.  In addition, we can get all of the objects attributes by using tab complete…

image 
So IPython is certainly more helpful than the normal Python interpreters interactive mode and likely something I’ll use as I continue to learn Python.  I would love to hear any other use cases people have for IPython as well!  I’m sure there are other features I have yet to uncover. 

Leave a Reply

Your email address will not be published. Required fields are marked *