Using the IPython REPL with PTVS

 

PTVS has a number of REPL modes to choose from:

 

REPL

Description

Editing / Debugging

Images

Standard

Default REPL, talks to Python directly

Edit: Standard Editing (arrows, multiline, etc).

Debug: No

No

IPython with Pylab

REPL talks to IPython backend

Edit: Standard + IPython commands + Pylab conveniences
Debug: No

Yes, inline in REPL

IPython w/o Pylab

REPL talks to IPython backend

Edit: Standard + IPython

Debug: No

Yes, separate window

Note that there’s also the “Debug REPL”, which is the standard REPL, but is aware of the current debugging context.

In this tutorial we’ll cover IPython/matplotlib/etc.

Installing IPython

We highly recommend installing IPython, matplotlib and their dependencies from a distro and not via PyPi. For this tutorial, we’ll use the package from UCI’s Python Extensions page:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-stack

Configuring IPython in PTVS

1. First let’s make sure IPython/matplotlib have been installed on your system & work properly.  Go to your Python installation directory and start IPython in Pylab mode (ipython --pylab), and enter the following:

x = linspace(0, 5, 10)
y = x ** 2
plot(x, y, 'r')

Assuming everything is configured properly, you should see something like this:

clip_image002_thumb[4]

2. Now let’s make sure PTVS can find all the right bits (IPython, matplotlib, etc):

- Bring up the Python Interpreters window by typing Ctrl-K-` (backtick). Here we see that Python 2.7 64-bit has been selected (Make Default):

clip_image004_thumb[2]

- Click the View in File Explorer link and make sure you can see IPython & matplotlib in your Lib\site-packages directory:

clip_image005_thumb[1]

3. On the Interpreters Windows, click the Interactive Options and set Interactive Mode to IPython:

clip_image007_thumb[1]

4. On the Interpreters Window, click the Interactive Window link to bring up the REPL window (or Alt-I). Now click the blue curved arrow (top left) to RESET the REPL from the previous mode (eg Standard to IPython mode):

clip_image008_thumb[1]

5.  Now we’re in IPython mode with Pylab. Let’s try to plot something. Enter this in the REPL:

x = linspace(0, 5, 10)
y = x ** 2
plot(x, y, 'r')

you should see a graph like this inline:

clip_image009_thumb[3]

If you hit enter again, you’ll see that the image becomes a thumbnail:

clip_image010_thumb[2]

However you can place the mouse pointer on the image and resize it if necessary:

clip_image011_thumb[2]

Entering, pasting and sending code to the REPL

Instead of typing in the REPL, you can select text in the Editor and use CTRL-E-E to send it to IPython. Try pasting this code into the Editor, then to the REPL via CTRL-A, CTRL-E-E:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]): 
    xs = np.arange(20) 
    ys = np.random.rand(20)

    # You can provide either a single color or an array. To demonstrate this,
    # the first bar of each set will be colored cyan.

    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

 

clip_image013_thumb[1]

When sending code to the Interactive window  (by using Ctrt-E-E), the commands are sent to IPython as one unit to avoid giving you intermediate “partial” graphs:

clip_image015_thumb[2]

So far all the images have been inline in the REPL window.  If you run the code from the Editor (CTRL-F5, or right click on the file name & choose Run w/o Debugging), you’ll get a standalone matplotlib window:

clip_image017_thumb[2]

Shell commands, magics, etc.

IPython has a number of useful features such as escaping to the system shell, variable substitution, capturing output, etc. Please refer to the IPython reference guide for further information:

clip_image019_thumb[1]

IPython notebook mode

You can also use IPython in “notebook” mode where you can use any browser on any OS as the canvas. The backend IPython engine can be local on your machine, or remote. Windows Azure has support for running IPython on a Windows or Linux VM.

clip_image021_thumb[2]

Please see the following for details:

Video: http://www.youtube.com/watch?v=ljrSOkMs7DQ

Docs: http://www.windowsazure.com/en-us/develop/python/tutorials/ipython-notebook/

 

Limitations

Note that IronPython does not support IPython, despite the fact that you can select it  on the Interactive Options form.  You can upvote the feature request or implement it if you’d like! https://pytools.codeplex.com/workitem/1632).

Last edited Sep 10, 2013 at 8:29 PM by huguesv, version 20

Comments

BoulderPip Dec 30, 2013 at 11:27 AM 
I'm getting network errors from http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-stack without end. I also failed to "pip" matplotlib et al manually on a python 3.3 x64 setup. Please improve the situation.