Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Jupyter says a magic like %%capture does not exist

Your error

UsageError: Line magic function `%%capture` not found.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

Something sits above %%capture in the cell

Make the %% line the first line of the cell.

Fix
%%capture
# install quietly
%pip install pandas
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

UsageError: Line magic function `%%capture` not found appears when %%capture is not the very first line of the cell, so IPython reads it as a line magic named %capture. Move it to line one, above any comment. With a single % the error adds a hint to use %%. Extension magics such as %%cython also need %load_ext first. Applies to IPython 8 and 9.18.

  • Applies to IPython 9.18.1 (Python 3.11+), IPython 8.18, Jupyter Notebook and JupyterLab with the ipykernel kernel
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run %lsmagic in a new cell. If the name is listed under cell magics, the problem is where or how you typed it (causes 1 and 2). If it is not listed at all, the code that provides it is not loaded in this kernel (cause 3).

Run this first
%lsmagic

Then find the line you see in this list and open its fix:

  1. 1Something sits above %%capture in the cellThe message shows two percent signs, `%%capture`, and has no hint in brackets after it
  2. 2A cell magic was typed with a single percent signThe message ends with (But cell magic `%%capture` exists, did you mean that instead?)
  3. 3The magic belongs to an extension that is not loaded%lsmagic does not list the name at all, and the message names a magic from another package, such as %%cython

What does the error look like?

  • UsageError: Line magic function `%%capture` not found.
  • UsageError: Line magic function `%capture` not found (But cell magic `%%capture` exists, did you mean that instead?).
  • UsageError: Cell magic `%%cython` not found.

Cause 1 of 3

Something sits above %%capture in the cell

You seeThe message shows two percent signs, `%%capture`, and has no hint in brackets after it

Check

Line magic function `%%capture` not found is raised when a comment, a blank line or code comes before the %% line. IPython's input transformer only checks the first line of a cell for a cell magic; any later line that starts with % is turned into a line magic call, and the leftover name %capture matches nothing.

Check
# broken: the comment makes %%capture line 2
# install quietly
%%capture
%pip install pandas

Fix

Make the %% line the first line of the cell. Move comments below it, or put them in a cell of their own.

Fix
%%capture
# install quietly
%pip install pandas

Verify

The cell runs without the UsageError and prints nothing, because %%capture swallowed the output.

Verify
%%capture out
print("hidden")

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

A cell magic was typed with a single percent sign

You seeThe message ends with (But cell magic `%%capture` exists, did you mean that instead?)

Check

IPython raises this variant when the line magic lookup fails but a cell magic with the same name exists. %%capture is cell-only, so %capture on any line is a call to something that is not defined; the shell's own code adds the hint in brackets.

Check
%lsmagic

Fix

Put the cell magic, with two percent signs, at the top of the cell that should run under it. %timeit is documented in both a line form and a %% cell form, which is why one percent works for it and not for %%capture.

Fix
%%capture captured
result = expensive_step()

Verify

captured.stdout holds the text the cell printed, and the UsageError is gone.

Verify
print(captured.stdout)

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The magic belongs to an extension that is not loaded

You see%lsmagic does not list the name at all, and the message names a magic from another package, such as %%cython

Check

Magics like %%cython come from extensions, not from IPython itself. Until the package is installed in the kernel's environment and loaded with %load_ext, the name is unknown and you get Cell magic not found, or Line magic function not found for the one-percent form.

Check
%pip show Cython

Fix

Install the package into the running kernel with %pip, then load its extension. IPython's extension docs give the name to load for Cython, which used to be bundled and now ships with Cython.

Fix
%pip install Cython
%load_ext Cython

Verify

%lsmagic now lists the magic under cell magics, and a %%cython cell compiles.

Verify
%%cython
def add(a, b):
    return a + b

Confirmed by ipython.readthedocs.io

Did this fix it?

How do I stop it coming back?

  • Keep each %% magic as line one of its own cell, and write the explanation in a Markdown cell above it.
  • Put %load_ext lines in the first cell of the notebook, so a restart and Run All loads every extension before it is used.
  • Install extension packages with %pip inside the notebook, which targets the kernel's own environment rather than whatever pip is on your PATH.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.