IPython Magic Commands in Jupyter Notebook (2026) - Part 10 | 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. %more¶
In Jupyter Notebook/IPython, %more is a magic command used to display the contents of a text file or object one screen at a time.
Useful for viewing large files without printing everything at once.
Similar to the Unix more command.
%more sample.txt
This is line 1 This is line 2 This is line 3
2. %pdb¶
In Jupyter Notebook/IPython, %pdb is a magic command used to automatically invoke the Python debugger after an exception occurs.
Helps in debugging errors interactively.
Can be turned ON or OFF.
%pdb on
Automatic pdb calling has been turned ON
a=1
b=0
c=a/b
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[3], line 3 1 a=1 2 b=0 ----> 3 c=a/b ZeroDivisionError: division by zero
[... skipping 1 hidden frame(s)] > c:\users\schol\appdata\local\temp\ipykernel_38896\3321636384.py(3)<module>()
1
0
*** ZeroDivisionError: division by zero
%pdb off
Automatic pdb calling has been turned OFF
3. %pprint¶
In Jupyter Notebook/IPython, %pprint is a magic command used to enable or disable pretty printing.
Makes complex data structures easier to read.
Useful for dictionaries, lists, and nested objects.
%pprint
Pretty printing has been turned ON
list(range(25))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
4. %psource¶
In Jupyter Notebook/IPython, %psource is a magic command used to display the source code of a Python object.
Works with functions, classes, and methods.
Helpful for understanding implementation details.
def add(a, b):
return a + b
%psource add
def add(a, b): return a + b
5. %pylab¶
In Jupyter Notebook/IPython, %pylab is a magic command used to load NumPy and Matplotlib into the interactive environment.
Provides quick access to scientific computing tools.
%pip install numpy matplotlib
Collecting numpy Using cached numpy-2.5.0-cp314-cp314-win_amd64.whl.metadata (6.6 kB) Collecting matplotlib Using cached matplotlib-3.11.0-cp314-cp314-win_amd64.whl.metadata (80 kB) Requirement already satisfied: contourpy>=1.0.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (1.3.3) Requirement already satisfied: cycler>=0.10 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (4.63.0) Requirement already satisfied: kiwisolver>=1.3.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (1.5.0) Requirement already satisfied: packaging>=20.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (26.0) Requirement already satisfied: pillow>=9 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (12.2.0) Requirement already satisfied: pyparsing>=3 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (3.3.2) Requirement already satisfied: python-dateutil>=2.7 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: six>=1.5 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0) Using cached numpy-2.5.0-cp314-cp314-win_amd64.whl (12.6 MB) Using cached matplotlib-3.11.0-cp314-cp314-win_amd64.whl (9.5 MB) Installing collected packages: numpy, matplotlib ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] ---------------------------------------- 0/2 [numpy] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] -------------------- ------------------- 1/2 [matplotlib] ---------------------------------------- 2/2 [matplotlib] Successfully installed matplotlib-3.11.0 numpy-2.5.0 Note: you may need to restart the kernel to use updated packages.
%pylab
Using matplotlib backend: module://matplotlib_inline.backend_inline %pylab is deprecated, use %matplotlib inline and import the required libraries. Populating the interactive namespace from numpy and matplotlib
C:\Users\schol\miniconda3\envs\jn\Lib\site-packages\IPython\core\magics\pylab.py:166: UserWarning: pylab import has clobbered these variables: ['add']
`%matplotlib` prevents importing * from pylab and numpy
warn("pylab import has clobbered these variables: %s" % clobbered +
x = linspace(0, 10, 100)
y = sin(x)
plot(x, y)
show()
6. %qtconsole¶
In Jupyter Notebook/IPython, %qtconsole is a magic command used to open a Qt-based IPython console connected to the current kernel.
Allows interaction with the same variables and session.
Useful for advanced debugging and multi-window workflows.
%pip install qtconsole
Collecting qtconsole Using cached qtconsole-5.7.2-py3-none-any.whl.metadata (4.4 kB) Requirement already satisfied: traitlets!=5.2.1,!=5.2.2 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (5.15.0) Requirement already satisfied: jupyter_core in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (5.9.1) Requirement already satisfied: jupyter_client>=4.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (8.9.1) Requirement already satisfied: pygments in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (2.20.0) Requirement already satisfied: ipykernel>=4.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (7.2.0) Requirement already satisfied: ipython_pygments_lexers in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (1.1.1) Requirement already satisfied: qtpy>=2.4.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (2.4.3) Requirement already satisfied: packaging in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from qtconsole) (26.0) Requirement already satisfied: comm>=0.1.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (0.2.3) Requirement already satisfied: debugpy>=1.6.5 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (1.8.16) Requirement already satisfied: ipython>=7.23.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (9.14.0) Requirement already satisfied: matplotlib-inline>=0.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (0.2.1) Requirement already satisfied: nest-asyncio>=1.4 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (1.5.1) Requirement already satisfied: psutil>=5.7 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (7.0.0) Requirement already satisfied: pyzmq>=25 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (27.1.0) Requirement already satisfied: tornado>=6.4.1 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipykernel>=4.1->qtconsole) (6.5.5) Requirement already satisfied: colorama>=0.4.4 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.4.6) Requirement already satisfied: decorator>=5.1.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipython>=7.23.1->ipykernel>=4.1->qtconsole) (5.3.1) Requirement already satisfied: jedi>=0.18.2 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.20.0) Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipython>=7.23.1->ipykernel>=4.1->qtconsole) (3.0.52) Requirement already satisfied: stack_data>=0.6.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.6.3) Requirement already satisfied: wcwidth in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.8.1) Requirement already satisfied: parso<0.9.0,>=0.8.6 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from jedi>=0.18.2->ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.8.7) Requirement already satisfied: python-dateutil>=2.8.2 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from jupyter_client>=4.1->qtconsole) (2.9.0.post0) Requirement already satisfied: typing-extensions>=4.13.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from jupyter_client>=4.1->qtconsole) (4.15.0) Requirement already satisfied: platformdirs>=2.5 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from jupyter_core->qtconsole) (4.9.4) Requirement already satisfied: six>=1.5 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from python-dateutil>=2.8.2->jupyter_client>=4.1->qtconsole) (1.17.0) Requirement already satisfied: executing>=1.2.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel>=4.1->qtconsole) (2.2.1) Requirement already satisfied: asttokens>=2.1.0 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel>=4.1->qtconsole) (3.0.1) Requirement already satisfied: pure_eval in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from stack_data>=0.6.0->ipython>=7.23.1->ipykernel>=4.1->qtconsole) (0.2.3) Using cached qtconsole-5.7.2-py3-none-any.whl (125 kB) Installing collected packages: qtconsole Successfully installed qtconsole-5.7.2 Note: you may need to restart the kernel to use updated packages.
qtc='Hello World'
%qtconsole
7. %rehashx¶
In Jupyter Notebook/IPython, %rehashx is a magic command used to update IPython's list of executable commands from the system PATH.
Creates aliases for executables found in the operating system.
Useful after installing new command-line tools.
%rehashx
8. %rep¶
In Jupyter Notebook/IPython, %rep is a magic command used to recall and execute code from previous inputs.
Helps avoid retyping commands.
Can retrieve commands from history.
%rep 1
%more sample.txt
This is line 1 This is line 2 This is line 3
9. %sc¶
In Jupyter Notebook/IPython, %sc (Shell Capture) is a magic command used to capture the output of shell commands into a Python variable.
Allows shell command results to be manipulated in Python.
Deprecated in newer IPython versions; !command with assignment is preferred.
%sc files = cd
files
'C:\\Users\\schol\\Projects\\Magic-Commands\r\n'
Comments
Post a Comment