Sitelet https://github.com/cartermp/visualstudio-docs/blob/master/docs/python/interactive-repl-ipython.md
Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 4.69 KB

File metadata and controls

83 lines (60 loc) · 4.69 KB
title IPython REPL (interactive window)
description Using the Visual Studio interactive window in IPython mode for a user-friendly interactive development environment with Interactive Parallel Computing features.
ms.date 06/19/2018
ms.prod visual-studio-dev15
ms.technology vs-python
ms.topic conceptual
author kraigb
ms.author kraigb
manager douge
ms.workload
python
data-science

Using IPython in the Interactive Window

The Visual Studio interactive window in IPython mode is an advanced yet user-friendly interactive development environment that has Interactive Parallel Computing features. This article walks through using IPython in the Visual Studio interactive window, in which all of the regular interactive window features are also available.

For this walkthrough you should have the Anaconda environment installed, which includes IPython and the necessary libraries.

Note

IronPython does not support IPython, despite the fact that you can select it on the Interactive Options form. FOr more information see the feature request.

  1. Open Visual Studio, switch to the Python Environments window (View > Other Windows > Python Environments), and select an Anaconda environment.

  2. Examine the Packages (Conda) tab (which may appear as pip or Packages) for that environment to make sure that ipython and matplotlib are listed. If not, install them here. (See Python Environments Windows - Packages tab.)

  3. Select the Overview tab and select Use IPython interactive mode. (In Visual Studio 2015, select Configure interactive options to open the Options dialog, then set Interactive Mode to IPython, and select OK).

  4. Select Open interactive window to bring up the interactive window in IPython mode. You may need to reset the window if you have just changed the interactive mode; you might also need to press Enter if only a >>> prompt appears, so that you get a prompt like "In [2]".

    The interactive window in IPython mode

  5. Enter the following code:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 5, 10)
y = x ** 2
plt.plot(x, y, 'r', x, x ** 3, 'g', x, x ** 4, 'b')
  1. After entering the last line, you should see an inline graph (which you can resize by dragging on the lower right-hand corner) if desired.

    Inline graph in the interactive window

  2. Instead of typing in the REPL, you can instead write code in the editor, select it, right-click, and select the Send to interactive command (or press Ctrl+Enter). Try pasting the code below into a new file in the editor, selecting it with Ctrl-A, then sending to the interactive window. (Visual Studio sends the code as one unit to avoid giving you intermediate or partial graphs. And if you don't have a Python project open with a different environment selected, Visual Studio opens an interactive window for whatever environment is selected as your default in the Python Environments window.)

    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 is 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()

    Sending code from the editor to the interactive window

  3. To see the graphs outside of the interactive window, run the code instead using the Debug > Start without Debugging command.

IPython has many other useful features such as escaping to the system shell, variable substitution, capturing output, etc. Refer to the IPython documentation for more.

Related articles