Using iPython step by step

Post date: Mar 16, 2014 1:46:42 AM

First install Python -- I use Python v 3.3.5 from https://www.python.org/download/

I want to use iPython notebook (http://ipython.org/notebook) for data analysis, and I found this webpage is extremely useful: http://nbviewer.ipython.org/github/jvns/talks/blob/master/pyconca2013/pistes-cyclables.ipynb

The webpage suggests that we should install from Anaconda. This is a very good short document.

Windows

This is a good tutorial how to install Anaconda on Windows: http://www.youtube.com/watch?v=oXnnRpTGT2M

Download the Anaconda package: http://continuum.io/downloads

Then just double click the exe/msi file, and proceed with next next next...

Once installed, you will see ipython v2.7 installed for you automatically as it comes with Anaconda this version.

To test the ipython

  1. run cmd
  2. >cd \Anaconda
  3. >ipython notebook
  4. wait 10 sec then you will be seeing ipython showing

Mac

Just download the install file, double click, done. Then you should try if it works. If command conda does not work, you might first want to check the path:

685b35c8f358:~ kittipat$ echo $PATH

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

for this problem, refer to this. Just add the new anaconda dir to the path, and it works:

685b35c8f358:~ kittipat$ export PATH=~/anaconda/bin:$PATH
685b35c8f358:~ kittipat$ echo $PATH
/Users/kittipat/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
685b35c8f358:~ kittipat$ conda
usage: conda [-h] [-V] command ...
conda is a tool for managing environments and packages.

Use Python 3.X from Anaconda

By default the python coming with Anaconda is version 2.7. To use Python 3.3, I follow the instruction in the section 'Starting from a regular Anaconda installation' of this page: http://continuum.io/blog/anaconda-python-3

  1. Create environmentPython 3.3 on Anaconda:
    1. >conda create -n py3k python=3 anaconda
    2. The program will download all necessary packages for python 3, similar to what it does for python 2.7. The example output is shown here.
  2. At the end of the process above, you can activate the environment by typing:
      1. ## For Windows >activate py3k ## for mac 685b35c8f358:python_dev kittipat$ source activate py3k discarding /Users/kittipat/anaconda/bin from PATH prepending /Users/kittipat/anaconda/envs/py3k/bin to PATH (py3k)685b35c8f358:python_dev kittipat$
  3. Now you can test ipython by typing the command 'ipython'
      1. [py3k] C:\Users\kittipat>ipython
      2. Python 3.3.4 |Anaconda 1.9.1 (64-bit)| (default, Feb 10 2014, 17:54:43) [MSC v.1
      3. 600 64 bit (AMD64)]
      4. Type "copyright", "credits" or "license" for more information.
      5. IPython 1.1.0 -- An enhanced Interactive Python.
      6. ? -> Introduction and overview of IPython's features.
      7. %quickref -> Quick reference.
      8. help -> Python's own help system.
      9. object? -> Details about 'object', use 'object??' for extra details.
      10. In [1]:
    1. Or you can type
      1. >ipython notebook
    2. Done! Now you can use ipython

If you want to install additional package in this new environment, say I want to to install package called 'matplotlib' on Python 3.3 environment, then I should do the following:

[py3k] C:\Users\kittipat>conda install matplotlib
Fetching package metadata: ..
Solving package specifications: ...
Package plan for installation in environment C:\Anaconda\envs\py3k:
The following packages will be downloaded:
package | build
---------------------------|-----------------
matplotlib-1.3.1 | np18py33_1 34.2 MB
python-3.3.5 | 0 21.6 MB
six-1.6.1 | py33_0 14 KB
------------------------------------------------------------
Total: 55.8 MB
The following packages will be UN-linked:
package | build
---------------------------|-----------------
python-3.3.4 | 0
six-1.5.2 | py33_0
The following packages will be linked:
package | build
---------------------------|-----------------
matplotlib-1.3.1 | np18py33_1 hard-link
python-3.3.5 | 0 hard-link
six-1.6.1 | py33_0 hard-link
Proceed ([y]/n)?

This is even a better way to install additional package: using

>pip install <package_name>

in the Anaconda environment, here is some example:

[py3k] C:\Users\kittipat>pip install urllib3
Downloading/unpacking urllib3
Running setup.py (path:c:\users\kittipat\appdata\local\temp\pip_build_kittipat
\urllib3\setup.py) egg_info for package urllib3
Installing collected packages: urllib3
Running setup.py install for urllib3
Successfully installed urllib3
Cleaning up...

However, if the package does not exist, it would show error message, and give suggestion...something like this.

[py3k] C:\Users\kittipat>pip install urllib2
Downloading/unpacking urllib2
Real name of requirement urllib2 is urllib3
Could not find any downloads that satisfy the requirement urllib2
Cleaning up...
No distributions at all found for urllib2
Storing debug log for failure in C:\Users\kittipat\pip\pip.log

If you want to show the figure plotted inline in the ipython notebook, you need to add --pylab inline

(see reference http://calebmadrigal.com/graph-ipython-notebook/)

>ipython notebook --pylab inline

And now you can plot and display figure inline!

How to setup your IDE to use Anaconda

http://docs.continuum.io/anaconda/ide_integration.html

Useful resource for Anaconda

http://docs.continuum.io/anaconda/faq.html

Python visualizer

http://pythontutor.com/visualize.html#

watching this video: http://www.youtube.com/watch?v=KZRrY9uzO7c

YOu can download ipython notebook and import into your own. Go to Github, download, and click import in your ipython notebook dashboard.