IPython Magic Commands in Jupyter Notebook (2026) - Part 11 | 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. %subshell¶
In IPython/Jupyter, %subshell is a magic command used to manage subshells.
A subshell is a separate execution environment within the same IPython session.
Useful for isolating commands and tasks.
Available only in newer IPython versions.
%subshell
subshell id: None thread id: 14592 main thread id: 14592 pid: 5216 thread count: 7 subshell list: []
2. %uv¶
In IPython, %uv is a magic command that integrates with the uv package manager.
Allows package installation and management directly from a notebook.
Provides a faster alternative to pip in supported environments.
Availability depends on the installed IPython version.
%pip install uv
Collecting uv
Downloading uv-0.11.24-py3-none-win_amd64.whl.metadata (12 kB)
Downloading uv-0.11.24-py3-none-win_amd64.whl (25.7 MB)
---------------------------------------- 0.0/25.7 MB ? eta -:--:--
---------------------------------------- 0.3/25.7 MB ? eta -:--:--
--------------------------------------- 0.5/25.7 MB 1.9 MB/s eta 0:00:14
- -------------------------------------- 1.0/25.7 MB 2.1 MB/s eta 0:00:12
-- ------------------------------------- 1.6/25.7 MB 2.3 MB/s eta 0:00:11
--- ------------------------------------ 2.4/25.7 MB 2.5 MB/s eta 0:00:10
---- ----------------------------------- 2.9/25.7 MB 2.6 MB/s eta 0:00:09
----- ---------------------------------- 3.7/25.7 MB 2.8 MB/s eta 0:00:08
------- -------------------------------- 4.7/25.7 MB 3.0 MB/s eta 0:00:07
-------- ------------------------------- 5.5/25.7 MB 3.2 MB/s eta 0:00:07
---------- ----------------------------- 6.6/25.7 MB 3.3 MB/s eta 0:00:06
----------- ---------------------------- 7.6/25.7 MB 3.5 MB/s eta 0:00:06
------------- -------------------------- 8.7/25.7 MB 3.6 MB/s eta 0:00:05
--------------- ------------------------ 10.2/25.7 MB 3.9 MB/s eta 0:00:05
----------------- ---------------------- 11.3/25.7 MB 4.0 MB/s eta 0:00:04
------------------- -------------------- 12.3/25.7 MB 4.0 MB/s eta 0:00:04
--------------------- ------------------ 13.6/25.7 MB 4.2 MB/s eta 0:00:03
----------------------- ---------------- 15.2/25.7 MB 4.4 MB/s eta 0:00:03
------------------------- -------------- 16.5/25.7 MB 4.5 MB/s eta 0:00:03
--------------------------- ------------ 17.8/25.7 MB 4.5 MB/s eta 0:00:02
----------------------------- ---------- 19.1/25.7 MB 4.6 MB/s eta 0:00:02
------------------------------- -------- 20.4/25.7 MB 4.7 MB/s eta 0:00:02
--------------------------------- ------ 21.5/25.7 MB 4.7 MB/s eta 0:00:01
----------------------------------- ---- 22.5/25.7 MB 4.8 MB/s eta 0:00:01
------------------------------------ --- 23.6/25.7 MB 4.8 MB/s eta 0:00:01
--------------------------------------- 25.2/25.7 MB 4.8 MB/s eta 0:00:01
---------------------------------------- 25.7/25.7 MB 4.7 MB/s 0:00:05
Installing collected packages: uv
Successfully installed uv-0.11.24
Note: you may need to restart the kernel to use updated packages.
%uv pip install pandas
Note: you may need to restart the kernel to use updated packages.
Using Python 3.14.6 environment at: C:\Users\schol\miniconda3\envs\jn Resolved 5 packages in 700ms Prepared 1 package in 10.56s Installed 1 package in 1.06s + numpy==2.5.0
3. %%!¶
In Jupyter Notebook/IPython, %%! is a cell magic used to execute an entire cell as a shell script.
Every line inside the cell is sent to the operating system shell.
Useful for running multiple shell commands together.
%%!
echo Hello
['Hello']
4. %%SVG¶
In Jupyter Notebook/IPython, %%SVG is a cell magic used to render SVG (Scalable Vector Graphics) content.
Displays SVG images directly in notebook output.
SVG images remain sharp when zoomed.
%%SVG
<svg height="100" width="100">
<circle cx="50" cy="50" r="40" fill="blue"/>
</svg>
5. %%svg¶
In Jupyter Notebook/IPython, %%svg is a cell magic used to render SVG graphics.
Functions similarly to %%SVG.
Allows embedding raw SVG markup directly in a notebook cell.
%%svg
<svg width="120" height="80">
<rect width="120" height="80" fill="green"/>
</svg>
6. %%cmd¶
In Jupyter Notebook on Windows, %%cmd is a cell magic used to run the entire cell in the Windows Command Prompt.
Useful for executing multiple Windows commands.
Available when supported by the environment.
%%cmd
dir
echo Hello
Microsoft Windows [Version 10.0.26200.8655]
(c) Microsoft Corporation. All rights reserved.
C:\Users\schol\Projects\Magic-Commands>dir
Volume in drive C is Windows-SSD
Volume Serial Number is 565A-BBE5
Directory of C:\Users\schol\Projects\Magic-Commands
06/25/2026 02:51 <DIR> .
05/25/2026 16:01 <DIR> ..
06/25/2026 01:16 <DIR> .ipynb_checkpoints
06/25/2026 01:14 31 copy.py
06/24/2026 16:45 16 demo.py
06/23/2026 02:01 22 hello.py
06/22/2026 23:39 35 hello.txt
06/01/2026 14:57 38 helper.py
05/26/2026 17:07 39,584 Magic-Commands-1.ipynb
06/25/2026 02:46 9,835 Magic-Commands-10.ipynb
06/25/2026 02:51 19,811 Magic-Commands-11.ipynb
06/22/2026 23:48 25,274 Magic-Commands-12.ipynb
06/17/2026 16:28 171,110 Magic-Commands-2.ipynb
05/29/2026 20:29 20,443 Magic-Commands-3.ipynb
06/01/2026 15:14 13,220 Magic-Commands-4.ipynb
06/02/2026 02:12 17,275 Magic-Commands-5.ipynb
06/11/2026 18:55 15,821 Magic-Commands-6.ipynb
06/24/2026 20:43 12,860 Magic-Commands-7.ipynb
06/25/2026 01:12 7,948 Magic-Commands-8.ipynb
06/25/2026 01:37 6,182 Magic-Commands-9.ipynb
06/23/2026 17:47 12,176 Magic-Commands-Checks.ipynb
06/24/2026 20:40 955 mylog.py~
05/29/2026 02:14 266 myscript.py
06/15/2026 02:10 48 my_script.py
06/19/2026 18:59 44 sample.txt
06/25/2026 01:14 <DIR> Test
06/23/2026 17:45 13 test.py
06/25/2026 02:22 <DIR> __pycache__
23 File(s) 373,007 bytes
5 Dir(s) 133,276,925,952 bytes free
C:\Users\schol\Projects\Magic-Commands>echo Hello
Hello
C:\Users\schol\Projects\Magic-Commands>
7. %%code_wrap¶
In IPython, %%code_wrap is a cell magic used to wrap code inside a template.
Primarily intended for extension developers.
Helps automate repetitive code generation.
%%code_wrap
print("Wrapped Start Code")
__code__
print("Wrapped End Code")
%code_wrap --list
Wrapped Start Code default Wrapped End Code
%code_wrap --list-all
Wrapped Start Code
default :
print('Wrapped Start Code')
__code__
print('Wrapped End Code')
Wrapped End Code
print("Test Code")
Wrapped Start Code Test Code Wrapped End Code
%code_wrap --remove
Wrapped Start Code Wrapped End Code
8. %%writefile¶
In Jupyter Notebook/IPython, %%writefile is a cell magic used to write the contents of a cell to a file.
Creates a new file or overwrites an existing file.
Useful for saving scripts directly from notebooks.
%%writefile hello.py
print("Hello World")
Writing hello.py
9. %%debug¶
In Jupyter Notebook/IPython, %%debug is a cell magic used to enter the Python debugger after an exception occurs.
Allows step-by-step inspection of variables and code execution.
Useful for troubleshooting notebook errors.
%%debug
1/0
NOTE: Enter 'c' at the ipdb> prompt to continue execution. > <string>(2)<module>()
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) File ~\miniconda3\envs\jn\Lib\bdb.py:913, in Bdb.run(self, cmd, globals, locals) 911 self.start_trace() 912 try: --> 913 exec(cmd, globals, locals) 914 except BdbQuit: 915 pass File <string>:2 1 'Could not get source, probably due dynamically evaluated source code.' ZeroDivisionError: division by zero
Comments
Post a Comment