Jupyter says a magic like %%capture does not exist
Fix it fast
Most common cause, 1 of 3Something sits above %%capture in the cell
Make the %% line the first line of the cell.
%%capture
# install quietly
%pip install pandasShort 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.
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).
%lsmagicThen find the line you see in this list and open its fix:
- 1Something sits above %%capture in the cell
The message shows two percent signs, `%%capture`, and has no hint in brackets after it - 2A cell magic was typed with a single percent sign
The message ends with (But cell magic `%%capture` exists, did you mean that instead?) - 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.
# broken: the comment makes %%capture line 2
# install quietly
%%capture
%pip install pandasFix
Make the %% line the first line of the cell. Move comments below it, or put them in a cell of their own.
%%capture
# install quietly
%pip install pandasVerify
The cell runs without the UsageError and prints nothing, because %%capture swallowed the output.
%%capture out
print("hidden")Confirmed by raw.githubusercontent.com
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.
%lsmagicFix
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.
%%capture captured
result = expensive_step()Verify
captured.stdout holds the text the cell printed, and the UsageError is gone.
print(captured.stdout)Confirmed by raw.githubusercontent.com
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.
%pip show CythonFix
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.
%pip install Cython
%load_ext CythonVerify
%lsmagic now lists the magic under cell magics, and a %%cython cell compiles.
%%cython
def add(a, b):
return a + bConfirmed by ipython.readthedocs.io
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.