Use IPython in the Interactive Window in Visual Studio
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 explains how to use IPython in the Visual Studio Interactive Window and access standard Interactive Window features.
Prerequisites
Visual Studio on Windows installed with support for Python workloads. For more information, see Install Python support in Visual Studio.
Visual Studio for Mac isn't supported. For more information, see What's happening to Visual Studio for Mac? Visual Studio Code on Windows, Mac, and Linux works well with Python through available extensions.
The Python installation must include the IPython, numpy, and matplotlib libraries. You can install these libraries by using the Package installer in Visual Studio, as described in Tutorial: Install packages in your Python environment in Visual Studio.
Note
IronPython doesn't support IPython, despite the fact that you can select this option on the Interactive Options form. For more information, see the Feature request (Support IPython when interp is IronPython).
Work with the Interactive Window
The following steps show you how to use IPython commands in the Interactive Window. This walkthrough assumes you're using Anaconda.
In Visual Studio, select View > Other Windows > Python Environments to open the Python Environments window.
In the Python Environments window, select an Anaconda environment.
Switch to the Packages view for the environment to see the installed packages. In the dropdown menu, select Packages (Conda). The menu option might be named pip or Packages.
Confirm that the
ipython
andmatplotlib
libraries are installed for the environment.If both libraries aren't installed, follow the instructions in Tutorial: Install packages in your Python environment in Visual Studio. For more information, see Python Environments window - Packages tabs reference.
Switch to the Overview view for the environment and select the Use IPython interactive mode option.
Select Open interactive window to show the Interactive Window in IPython mode. You might need to reset the window if the window was already open.
When the Interactive Window opens, you should see the first prompt
In [1]
. If you see the standard prompt>>>
, enter return at the prompt to ensure the window is using IPython mode. The prompt should change to something likeIn [2]
.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')
After you enter the last line of code and select Enter, you should see an inline graph:
You can resize the graph by dragging on the lower right-hand corner.
Rather than entering code directly in the Interactive Window, you can write code in the Visual Studio editor and send it to the Interactive Window:
Paste the following code into a new file in the editor.
Use the keyboard shortcut Ctrl+A to select the code in the editor.
Right-click the selected code and choose Send to Interactive. You can also use the keyboard shortcut Ctrl+Enter.
Visual Studio sends the code as a single block to the Interactive Window to avoid an intermediate or partial graph.
(If you don't have a Python project open with a specific active environment, Visual Studio opens an Interactive Window for the default environment listed 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()
To view the graphs outside of the Interactive Window, run the code with the Debug > Start without Debugging command on the main Visual Studio toolbar.
IPython has many other useful features, such as escaping to the system shell, variable substitution, capturing output, and so on. For more information, see the IPython documentation.