Saturday December 15, 2007 12:48

Interactive Python shell with IPython

Posted by shakir as Information Insemination





One nice thing about Python as compared to some other scripting languages, is that it has a shell that enables us to test our Python script on the fly without needing to write a script file first

It’s nice and all that, but when you’re too used to IDEs which has features such as auto completion etcetera, you’ll feel like something is not right about the default python shell.

That changes when I come to IPython, one of the few other enhanced Python shell. The one feature I like most is the auto complete feature, which among other by using it I don’t need to remember module names, and at the same time would list and try Python modules that I’ve never heard of.

In Ubuntu, or debian in general, the package is called ipython, and to install it just;

shakir@herugrim ~ $ sudo apt-get install ipython

Can you spot the differences between this

shakir@herugrim ~ $ python
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import command
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named command
>>> import commands
>>> status, output = commands.getoutputstatus ('ls')
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'getoutputstatus'
>>> status, output = commands.getstatusoutput ('ls')
>>> print output
Desktop
Documents
Music
Photos
>>>
shakir@herugrim ~ $

and this?

shakir@herugrim ~ $ ipython
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import command
---------------------------------------------------------------------------
           Traceback (most recent call last)

/home/shakir/ in ()

: No module named command

In [2]: import commands

In [3]: status, output = commands.
commands.__all__           commands.__hash__          commands.__setattr__
commands.__class__         commands.__init__          commands.__str__
commands.__delattr__       commands.__name__          commands.getoutput
commands.__dict__          commands.__new__           commands.getstatus
commands.__doc__           commands.__reduce__        commands.getstatusoutput
commands.__file__          commands.__reduce_ex__     commands.mk2arg
commands.__getattribute__  commands.__repr__          commands.mkarg

In [3]: status, output = commands.gets
commands.getstatus        commands.getstatusoutput

In [3]: status, output = commands.getstatusoutput('ls')

In [4]: print output
Desktop
Documents
Music
Photos

In [5]:
Do you really want to exit ([y]/n)? y
shakir@herugrim ~ $

I hope you can guess when did I press the [tab] key :)





Tags: ,

Comment Form