IPython Magic Commands in Jupyter Notebook (2026) - Part 8 | Step-by-Step Guide
Magic Commands¶
In Jupyter Notebook, Magic Commands are special built-in commands that provide additional functionality beyond normal Python commands.
They help users perform tasks such as file handling, timing code execution, managing variables, and controlling the notebook environment more easily.
Magic commands usually start with % for line magics and %% for cell magics.
They are designed to make working in Jupyter Notebook faster and more interactive.
1. %autocall¶
Automatically adds parentheses when calling functions.
Makes function calls more convenient in interactive sessions.
Has three modes:
0 = Off
1 = Smart
2 = Full
len("Hello")
5
%autocall 2
Automatic calling is: Full
len "Hello"
------> len("Hello")
------> len("Hello")
5
2. %pdef¶
Displays the function signature (definition) of an object.
Useful for checking parameters accepted by a function.
Similar to viewing a function prototype.
%pdef print
print(*args, sep=' ', end='\n', file=None, flush=False)
3. %pdoc¶
Shows the documentation (docstring) of an object.
Helps understand what a function, class, or module does.
Similar to Python's help() function.
%pdoc print
Class docstring: Prints the values to a stream, or to sys.stdout by default. sep string inserted between values, default a space. end string appended after the last value, default a newline. file a file-like object (stream); defaults to the current sys.stdout. flush whether to forcibly flush the stream. Call docstring: Call self as a function.
4. %pfile¶
Displays the source file where an object is defined.
Opens and shows the contents of the corresponding Python file.
Useful for inspecting implementation details.
import statistics
%pfile statistics.mean
5. %psearch¶
Searches for objects matching a pattern.
Useful when you remember only part of a function or variable name.
Supports wildcard searches.
%psearch st*
staticmethod statistics str
6. %autoawait¶
Controls automatic handling of await expressions.
Useful when working with asynchronous code (async/await).
Commonly used in Jupyter notebooks.
%autoawait on
import asyncio
await asyncio.sleep(1)
print("Done")
Done
7. %autosave¶
Sets the notebook's autosave interval.
Time is specified in seconds.
Helps prevent loss of work.
%autosave 60
Autosaving every 60 seconds
8. %code_wrap¶
Wraps selected code in a predefined template.
Useful for quickly creating functions, loops, exception blocks, etc.
Primarily available in IPython/Jupyter environments that support code templates.
%%code_wrap test
print('test')
__code__
%code_wrap --list-all
test
test :
print('test')
__code__
print("Hello")
test Hello
%code_wrap --remove test
test
Comments
Post a Comment