Szerkesztés

Megosztás a következőn keresztül:


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

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.

  1. In Visual Studio, select View > Other Windows > Python Environments to open the Python Environments window.

  2. In the Python Environments window, select an Anaconda environment.

  3. 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.

  4. Confirm that the ipython and matplotlib 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.

  5. Switch to the Overview view for the environment and select the Use IPython interactive mode option.

  6. 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.

  7. 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 like In [2].

    Screenshot that shows the Interactive Window in IPython mode in Visual Studio.

  8. 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')
    
  9. After you enter the last line of code and select Enter, you should see an inline graph:

    Screenshot that shows an inline graph in the Interactive Window in IPython mode in Visual Studio.

    You can resize the graph by dragging on the lower right-hand corner.

  10. 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:

    1. Paste the following code into a new file in the editor.

    2. Use the keyboard shortcut Ctrl+A to select the code in the editor.

    3. 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()
    

    Screenshot that shows how to send code from the Visual Studio editor to the Interactive Window.

  11. 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.