Why do we need IPython?
The
project IPython aims at making python more interactive. The default
interactive python shell you get when you type "python" is very limited
with functionality. For example, we can not explore files and
directories with "ls" command or you have module imported and want to
see what is it there is no easy option. Also error messages with
exploring files are not very informative.
How to install?
Make sure you have PIP already setup which will help to install other libraries easily. If you don't have it then refer this.
> pip install ipython
Install few other useful packages
> pip install nose
> pip install pexpect
> pip install pandoc
For IPython notebook
> pip install pyzmq
> pip install Tornado # This project is for HTTP server.
> pip install Jinja # This is templating tool to render HTML pages.
If you get error that some specific module is missing then install it using PIP, for example if you get following error: "ImportError: No module named jinja2" then use,
> pip install Jinja2
Starting IPython
If
you have installed all packages successfully then just type ipython on
command prompt, which will open ipython interactive session. You should
be able to see something like,
This
is similar to interactive shell of python but rich in features, so lets
try few basic python commands. Declare few variables,
# Lets declare 2 integer variables and one string variable
> varA = 10
> varB = 20
> myName = 'Akshay'
If you want to get list of all variable you can use the "who" command.
# To get list of all variables use,
> who
# Then to get variables with type integer use,
> who int
# To get list of variables with name, type and value use,
> whos
Now
lets explore on how to clear variables from session. Use "reset"
command which will clear all variables currently available in memory.
# To check existing objects,
> who
# To reset the session and delete all variables,
> reset
# To crosscheck if all objects (variables) are deleted
> who
# We can enable logging by following command,
> logstart
# To switch off logging,
> logoff
# To switch on logging.
> logon
Now
there are many other useful commands similar to what we have seen
above. These commands are called magic commands and get the list of
command,
> lsmagic
IPython
has predefined ‘magic functions’ which can be used as
command line style syntax. There are two kinds of magics as you can see
in screenshot above, line-oriented magics and cell-oriented magics.
- Line magics are prefixed with single % character and work
like regular command-line calls: they get as an argument the rest of the line, where
arguments are passed without parentheses or quotes.
- Cell magics are
prefixed with a double %%, and they are functions that get as an argument
not only the rest of the line, but also the lines below it in a separate
argument.