IPython Magic Commands in Jupyter Notebook (2026) - Part 6 | 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. %colors¶
Changes the color scheme used by IPython/Jupyter Notebook.
Improves readability of prompts, errors, and syntax highlighting.
Common schemes: Linux, Neutral, NoColor.
Affects only the current interactive session.
%config ZMQInteractiveShell.colors
'neutral'
%colors nocolor
1/0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[3], line 1 ----> 1 1/0 ZeroDivisionError: division by zero
2. %xmode¶
Controls the level of detail shown in exception tracebacks.
Useful for debugging errors.
Modes:
Plain → Minimal traceback.
Context → Shows surrounding code.
Verbose → Detailed traceback with variables.
from IPython import get_ipython
get_ipython().InteractiveTB.mode
'Context'
a=1
b=0
c=a/b
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[5], line 3
1 a=1
2 b=0
----> 3 c=a/b
ZeroDivisionError: division by zero
%xmode Verbose
Exception reporting mode: Verbose
%tb
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[5], line 3
1 a=1
2 b=0
----> 3 c=a/b
a = 1
b = 0
ZeroDivisionError: division by zero
3. %pip¶
Installs Python packages directly from a notebook.
Ensures packages are installed in the current kernel environment.
Preferred over using !pip.
%pip install numpy
Collecting numpy Using cached numpy-2.4.6-cp312-cp312-win_amd64.whl.metadata (6.6 kB) Using cached numpy-2.4.6-cp312-cp312-win_amd64.whl (12.3 MB) Installing collected packages: numpy Successfully installed numpy-2.4.6 Note: you may need to restart the kernel to use updated packages.
4. %hist¶
In Jupyter Notebook/IPython, %hist is a magic command used to display the command history of the current session.
It shows previously executed commands, making it easy to review, reuse, or save code.
%hist is short for history.
You can display all commands or a specific range of commands.
x = 10
y = 20
print(x + y)
30
%hist
%config ZMQInteractiveShell.colors %colors nocolor 1/0 from IPython import get_ipython get_ipython().InteractiveTB.mode a=1 b=0 c=a/b %xmode Verbose %tb %pip install numpy x = 10 y = 20 print(x + y) %hist
5. %reset_selective¶
Removes selected variables from memory.
Uses regular expressions to match variable names.
Safer than %reset, which clears everything.
a = 10
b = 20
%reset_selective a
a
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[13], line 1 ----> 1 a NameError: name 'a' is not defined
6. %alias_magic¶
Creates a shortcut (alias) for an existing magic command %timeit which is used to measure the average execution time of a code statement.
Helps reduce typing for frequently used magics.
Works with both line and cell magics.
%timeit sum(range(1000))
26.1 μs ± 652 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%alias_magic t timeit
Created `%t` as an alias for `%timeit`. Created `%%t` as an alias for `%%timeit`.
%t sum(range(1000))
25.4 μs ± 992 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
7. %macro¶
Saves previously executed commands into a reusable macro.
Useful for repeating a sequence of commands.
Macros can be executed like variables.
%macro mymacro 1
Macro `mymacro` created. To execute, type its name (without quotes).
=== Macro contents: ===
get_ipython().run_line_magic('config', 'ZMQInteractiveShell.colors')
mymacro
'nocolor'
8. %who_ls¶
Returns a list of variables currently defined.
Unlike %who, it provides the result as a Python list.
Useful for programmatic inspection.
x = 10
y = "Hello"
%who
b get_ipython mymacro x y
%who_ls
['b', 'get_ipython', 'mymacro', 'x', 'y']
9. %mkdir¶
Creates a new directory (folder).
Similar to the shell command mkdir.
Useful for creating folders directly from a Jupyter Notebook.
%mkdir myfolder
10. %rmdir¶
In Jupyter Notebook, %rmdir is a magic command used to remove an empty directory.
It stands for 'Remove Directory'.
It deletes the specified folder, but only if it has no files or subfolders inside.
If the directory is not empty, it will show an error.
%rmdir myfolder
Comments
Post a Comment