REW

How To Step Through Python Code In Jupyter Notebook?

Published Aug 29, 2025 5 min read
On this page

There are three primary methods for stepping through Python code in a Jupyter Notebook: using the built-in visual debugger in JupyterLab, using the IPython magic commands %debug and ipdb.set_trace() for text-based debugging, and using an external IDE like Visual Studio Code for a more integrated experience.

Method 1: The visual debugger (JupyterLab)

The visual debugger is the most user-friendly and modern method, available by default in JupyterLab since version 3.0. It provides a graphical interface similar to a traditional IDE, allowing you to set breakpoints and inspect variables easily.

Requirements

  • JupyterLab 3.0+
  • ipykernel 6.0+ (most modern installations include this by default)

How to use

  1. Open the debugger panel: In your JupyterLab notebook, click the bug icon ( ) in the top-right corner of the interface to toggle the debugger panel.
  2. Activate the debugger: A larger bug icon will appear in the right-hand sidebar. Click this icon to open the debugger interface and enable debugging for the notebook.
  3. Set breakpoints: Click in the grey gutter to the left of a line number in a code cell to set a breakpoint (a red dot will appear).
  4. Run the code: Execute the cell containing your breakpoint. The code will pause just before running the line with the breakpoint. The debugger panel will show:
    • Variable explorer: A list of variables and their current values.
    • Call stack: The sequence of function calls that led to the current line.
    • Breakpoints: A list of all breakpoints you've set.
  5. Step through code: Use the controls at the top of the debugger panel to control execution:
    • Step Over: Execute the current line and move to the next. If the line is a function call, it executes the entire function without stepping into it.
    • Step Into: Step inside the function call on the current line to debug it line by line.
    • Step Out: Exit the current function and return to the calling line.
    • Continue: Resume normal execution until the next breakpoint or the end of the code.

Method 2: IPython magic commands

This method uses the Python standard library debugger, pdb, which is integrated into Jupyter via the ipdb (the IPython debugger) module. It provides a text-based interface at the bottom of your notebook cell.

Using the %debug magic command

The %debug magic command is useful for "post-mortem" debugging, where the debugger is activated only after an error has occurred in a code cell.

  1. Cause an error: Run a cell that you know will produce an error, or just run your normal code until an exception is raised.
  2. Activate the debugger: In a new cell, type and run %debug.
  3. Enter the debugger: The interactive ipdb prompt will appear at the bottom of the output for the cell that crashed.
  4. Inspect and navigate: At the ipdb> prompt, you can use the following commands:
    • n (next): Execute the current line and move to the next line in the current function.
    • s (step): Step into a function call on the current line.
    • c (continue): Continue execution until the next breakpoint or the program ends.
    • p <variable> (print): Print the value of a variable.
    • q (quit): Quit the debugger.
    • h (help): Show a list of available commands.

Using ipdb.set_trace()

This method allows you to set a breakpoint at a specific line in your code, just as you would with the visual debugger.

  1. Import the module: Start by importing the ipdb library in a code cell.python

    import ipdb
    

    Use code with caution.

  2. Add a breakpoint: Place ipdb.set_trace() on the line where you want execution to pause.python

    def calculate_sum(a, b):
        ipdb.set_trace() # Execution will pause here
        result = a + b
        return result
    calculate_sum(10, 20)
    

    Use code with caution.

  3. Run the cell: When you run the cell, the ipdb> prompt will appear at the bottom of the cell output, and you can use the same commands as with the %debug magic command.

The %%debug magic command

If you want to debug an entire cell from the start, place %%debug at the very top of the cell. This is especially useful for quickly debugging a small, self-contained block of code.

%%debug
def my_function(x):
    y = x + 1
    z = y * 2
    return z
my_function(5)

Use code with caution.

Method 3: Visual Studio Code

For users who prefer a full-featured IDE, VS Code offers robust and powerful Jupyter Notebook debugging capabilities. This provides the best of both worlds: the interactive nature of notebooks with the full power of a desktop debugger.

Requirements

  • Visual Studio Code with the Python and Jupyter extensions installed.
  • ipykernel 6.0+.

How to use

  1. Open the notebook: Open your .ipynb file directly in VS Code.
  2. Set breakpoints: Click in the gutter to the left of the line numbers in a code cell to set a breakpoint (a red dot will appear).
  3. Run in debug mode:
    • Debug the entire notebook: Click the Debug button at the top of the notebook toolbar.
    • Debug a single cell: Hover over the code cell and click the Debug Cell button that appears.
  4. Use the VS Code debugger UI: When a breakpoint is hit, the VS Code debugger sidebar will open, showing:
    • Variables: Inspect the values of all variables in the current scope.
    • Call Stack: Navigate through the active function calls.
    • Breakpoints: Manage your breakpoints.
    • Watch: Add expressions to watch their values dynamically.
  5. Control execution: Use the familiar VS Code debug controls at the top of the editor:
    • Continue (F5)
    • Step Over (F10)
    • Step Into (F11)
    • Step Out (Shift+F11)
  6. Use the Debug Console: The debug console at the bottom of the screen allows you to run Python commands and evaluate expressions in the current debugging context.

Summary: Choosing the right method

Feature Visual Debugger (JupyterLab) IPython Magic (%debug, ipdb.set_trace) Visual Studio Code
Interface Graphical UI in JupyterLab Text-based prompt in notebook output Full-featured IDE interface
Ease of Use Very easy, point-and-click breakpoints. Easy, but requires knowledge of debugger commands. Very easy, familiar to IDE users.
Installation Included by default in recent JupyterLab. Included with IPython/Jupyter. Requires installing VS Code, Python, and Jupyter extensions.
Best For Modern data science, interactive debugging, beginners. Quick post-mortem debugging, scripting, quick checks. Production-grade code, complex projects, users who prefer IDEs.
Enjoyed this article? Share it with a friend.