PyCharm 2019.1 Help

Running and Debugging Jupyter Notebook Cells

Code fragments in a Jupyter notebook file are structured as executable cells. Each cell is marked with the #%% comment and can be executed independently by clicking the Run cell icon in the left gutter.

Run a code cell

For the target cell press Ctrl+Enter. Alternatively, you can click the Execute cell icon and select Run Cell. PyCharm launches the Jupyter server, executes the code cell, and renders the output in the Preview pane.

Currently, you can execute only one cell at a time, so mind code dependencies when executing cells. For example, the second cell in the code fragment uses the variables defined in the first cell. So, if you modify the N value, the changes will be reflected in the scatter chart only after the first cell gets executed. You can use smart shortcuts to execute the current cell and select the below one: Shift+Enter for Windows or ⇧↩ for macOS .

When you stop the server and change the server or kernel, you have to execute all cells with dependencies again, because execution results are valid for the current server session only.

View variables

When you execute a cell, the Variables tab of the Jupyter server tool window opens automatically.

Jupyter server tool window: the Variables tab

By default, variables are loaded asynchronously. To change the loading policy, click Manage the loading policy in the Variables tab, select Variables Loading Policy, and select one of the available modes. See Managing Variables Loading Policy for more details.

You can click the View as Array link to preview values for a particular variable in the tabular form:

Viewing variables as array

Note that Variables tab will appear each time you execute a cell, so if, by some reasons, you need to close it permanently, deselect the corresponding option in the project settings (Settings/Preferences | Build, Execution, Deployment | Jupyter).

When you work with a substantial number of code cells, you can effectively navigate between the Editor and Preview panes by using auto scrolling.

Jump to the target output fragment or target code cell

  1. Enable auto scrolling from the source by clicking the Auto scroll from Source icon, then select a code cell in the Editor pane. You will be positioned on the target output fragment in the Preview pane.

  2. Enable auto scrolling to the source code by clicking the Auto scroll to Source icon on the Jupyter toolbar, then select an arbitrary output fragment in the Preview pane. You'll be positioned on the source code cell in the Editor pane.

PyCharm provides the full-functional Jupyter Notebook Debugger.

Debug code in Jupyter notebook

  1. Set the breakpoints in the selected cell and press Shift+Alt+Enter for Windows or ⌥⇧↩ for macOS. Alternatively, you can click the Run cell icon, and select Debug Cell.

    The Jupyter Notebook Debugger tool window opens.

    Jupyter Notebook Debugger tool window
  2. Use the stepping toolbar buttons to choose on which line you want to stop next and switch to the Debugger tab to preview the variable values:

    Stepping over in the Jupyter Notebook Debugger

    Debugging is performed within a single code cell. However, if your code cell calls a function from any cell that has been already debugged, you can step into it. The related breakpoints will also work. Note that the cell with the function must be debugged not just executed.

    Stepping into the previous cell

    Similarly, you can step into a function called from a Python file that is located in the same project.

    Stepping into a Python file
  3. Proceed with the debugging steps to complete the execution of the cell.

    Debugging is complete

With PyCharm you can always quickly preview reference documentation for a particular variable, type, or argument.

Preview reference documentation

To view reference information for any element of a particular code cell:

  1. Place the caret within the target code cell and type ? <type/variable/argument>. (in this example, you will preview documentation for plt.scatter). Note that a code element should be accessible within the code cell.

  2. Execute the cell. The Introspection tab opens in the Jupyter tool window.

    Previewing reference documentation for plt.scatter

  3. Preview reference documentation in the Introspection tab.

Note that the Introspection tab shows documentation for the latest requested code element. Even though you proceed with executing other code cells, restart the server, or delete the line with your request, this information will be shown.

Last modified: 17 July 2019

See Also

External Links: