IPython Magic Commands in Jupyter Notebook (2026) - Part 12 | 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. %%system¶
In Jupyter Notebook/IPython, %%system is a cell magic used to run operating system commands.
Executes all lines in the cell as system commands.
Similar to using ! for shell commands, but applies to the entire cell.
Useful for automation and system administration tasks.
%%system
echo Hello
['Hello']
2. %%time¶
In Jupyter Notebook/IPython, %%time is a cell magic used to measure the execution time of an entire cell.
Displays CPU time and wall-clock time.
Useful for quickly checking code performance.
Executes the code only once.
%%time
sum(range(1000000))
CPU times: total: 46.9 ms Wall time: 233 ms
499999500000
3. %%timeit¶
In Jupyter Notebook/IPython, %%timeit is a cell magic used to measure code execution speed accurately.
Runs the code multiple times automatically.
Provides average execution time and standard deviation.
More reliable than %%time for benchmarking.
%%timeit
sum(range(1000000))
42.5 ms ± 3.44 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
4. %conda¶
In Jupyter Notebook, %conda is a magic command used to run Conda package management commands directly from a notebook cell.
It helps install, update, remove, and manage packages without leaving the notebook.
Works with environments managed by Conda.
Useful for ensuring packages are installed in the same environment that the notebook kernel is using.
%conda list
# packages in environment at C:\Users\schol\miniconda3\envs\jn: # # Name Version Build Channel anyio 4.12.1 py314haa95532_0 argon2-cffi 25.1.0 py314haa95532_0 argon2-cffi-bindings 25.1.0 py314h02ab6af_0 args 0.1.0 pypi_0 pypi asttokens 3.0.1 py314haa95532_0 async-lru 2.0.5 py314haa95532_0 attrs 26.1.0 py314h35db113_0 babel 2.17.0 py314haa95532_0 beautifulsoup4 4.15.0 py314haa95532_0 black 26.5.1 pypi_0 pypi bleach 6.3.0 py314haa95532_1 brotlicffi 1.2.0.0 py314h885b0b7_0 bzip2 1.0.8 h2bbff1b_6 ca-certificates 2026.5.14 haa95532_0 certifi 2026.6.17 py314haa95532_0 cffi 2.0.0 py314h02ab6af_1 charset-normalizer 3.4.4 py314haa95532_0 click 8.4.1 pypi_0 pypi clint 0.5.1 pypi_0 pypi colorama 0.4.6 py314haa95532_0 comm 0.2.3 py314haa95532_0 conpty 1.23.20211.0 h169ad4e_1 contourpy 1.3.3 pypi_0 pypi coverage 7.14.3 pypi_0 pypi cycler 0.12.1 pypi_0 pypi debugpy 1.8.16 py314h885b0b7_1 decorator 5.3.1 py314haa95532_0 defusedxml 0.7.1 pyhd3eb1b0_0 executing 2.2.1 py314haa95532_0 fonttools 4.63.0 pypi_0 pypi greenlet 3.5.2 pypi_0 pypi h11 0.16.0 py314haa95532_1 html5lib 1.1 pyhd3eb1b0_0 httpcore 1.0.9 py314haa95532_0 httpx 0.28.1 py314haa95532_1 idna 3.18 py314haa95532_0 ipykernel 7.2.0 py314h56ec5ee_0 ipython 9.14.0 py314haa95532_0 ipython-genutils 0.2.0 pypi_0 pypi ipython-sql 0.5.0 pypi_0 pypi ipython_pygments_lexers 1.1.1 py314haa95532_0 jedi 0.20.0 py314haa95532_0 jinja2 3.1.6 py314haa95532_0 json5 0.12.1 py314haa95532_0 jsonschema 4.25.1 py314haa95532_0 jsonschema-specifications 2025.9.1 py314haa95532_0 jupyter-lsp 2.3.0 py314haa95532_0 jupyter_client 8.9.1 py314haa95532_0 jupyter_core 5.9.1 py314haa95532_0 jupyter_events 0.12.1 py314haa95532_0 jupyter_server 2.18.2 py314haa95532_0 jupyter_server_terminals 0.5.4 py314haa95532_0 jupyterlab 4.5.7 py314haa95532_0 jupyterlab_pygments 0.3.0 py314haa95532_0 jupyterlab_server 2.28.0 py314haa95532_1 kiwisolver 1.5.0 pypi_0 pypi libexpat 2.8.1 hd7fb8db_1 libffi 3.4.8 h42d73b9_3 libmpdec 4.0.1 h1c6eee0_0 libsodium 1.0.21 h19b670b_0 libzlib 1.3.2 h1c6eee0_0 lz4-c 1.9.4 h2bbff1b_1 mamba 0.11.3 pypi_0 pypi markupsafe 3.0.2 py314h827c3e9_0 matplotlib-inline 0.2.1 py314haa95532_0 mistune 3.2.1 py314haa95532_0 mypy-extensions 1.1.0 pypi_0 pypi nbclient 0.10.4 py314haa95532_0 nbconvert-core 7.17.0 py314haa95532_0 nbformat 5.10.4 py314haa95532_0 nest-asyncio 1.5.1 pyhd3eb1b0_0 notebook 7.5.6 py314haa95532_0 notebook-shim 0.2.4 py314haa95532_1 numpy 2.5.0 pypi_0 pypi openssl 3.5.7 hbb43b14_0 packaging 26.0 py314haa95532_0 pandas 3.0.3 pypi_0 pypi pandocfilters 1.5.1 py314haa95532_0 parso 0.8.7 py314haa95532_0 pathspec 1.1.1 pypi_0 pypi pillow 12.2.0 pypi_0 pypi pip 26.1.1 pyh0d26453_1 platformdirs 4.9.4 py314haa95532_0 prettytable 3.18.0 pypi_0 pypi prometheus_client 0.24.1 py314haa95532_0 prompt-toolkit 3.0.52 py314haa95532_1 prompt_toolkit 3.0.52 hd3eb1b0_1 psutil 7.0.0 py314h02ab6af_1 pure_eval 0.2.3 py314haa95532_0 pycparser 3.0 py314haa95532_0 pygments 2.20.0 py314haa95532_0 pyparsing 3.3.2 pypi_0 pypi pyqt5-qt5 5.15.2 pypi_0 pypi pyqt5-sip 12.18.0 pypi_0 pypi pysocks 1.7.1 py314haa95532_1 python 3.14.6 hb107ab2_100_cp314 python-dateutil 2.9.0post0 py314haa95532_2 python-fastjsonschema 2.21.2 py314haa95532_0 python-json-logger 4.0.0 py314haa95532_0 python_abi 3.14 2_cp314 pytokens 0.4.1 pypi_0 pypi pywin32 311 py314h885b0b7_0 pywinpty 3.0.2 py314hbe95b07_0 pyyaml 6.0.3 py314hb9a58be_0 pyzmq 27.1.0 py314h7149c55_1 qtpy 2.4.3 pypi_0 pypi referencing 0.37.0 py314haa95532_0 requests 2.34.2 py314haa95532_0 rfc3339-validator 0.1.4 py314haa95532_0 rfc3986-validator 0.1.1 py314haa95532_0 rpds-py 0.28.0 py314h114bc41_0 send2trash 1.8.3 py314haa95532_0 setuptools 82.0.1 py314haa95532_0 six 1.17.0 py314haa95532_0 soupsieve 2.5 py314haa95532_0 sqlalchemy 2.0.51 pypi_0 pypi sqlite 3.53.2 hee5a0db_0 sqlparse 0.5.5 pypi_0 pypi stack_data 0.6.3 py314haa95532_0 terminado 0.18.1 py314haa95532_1 tinycss2 1.5.1 py314haa95532_0 tk 8.6.15 hf199647_0 tornado 6.5.5 py314h1c6eee0_0 traitlets 5.15.0 py314haa95532_0 typing-extensions 4.15.0 py314haa95532_0 typing_extensions 4.15.0 py314haa95532_0 tzdata 2026.2 pypi_0 pypi ucrt 10.0.22621.0 haa95532_0 urllib3 2.7.0 py314haa95532_0 uv 0.11.24 pypi_0 pypi vc 14.3 h2df5915_12 vc14_runtime 14.44.35208 h4927774_12 vs2015_runtime 14.44.35208 ha6b5a95_12 wcwidth 0.8.1 pypi_0 pypi webencodings 0.5.1 py314haa95532_2 websocket-client 1.8.0 py314haa95532_0 win_inet_pton 1.1.0 py314haa95532_1 winpty 0.4.3 haa95532_5 xz 5.8.2 h53af0af_0 yaml 0.2.5 he774522_0 zeromq 4.3.5 h6a984cb_2 zlib 1.3.2 h1c6eee0_0 zstd 1.5.7 h56299aa_0 Note: you may need to restart the kernel to use updated packages.
%conda install numpy
Jupyter detected...
3 channel Terms of Service accepted
Channels:
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\schol\miniconda3\envs\jn
added / updated specs:
- numpy
The following NEW packages will be INSTALLED:
blas pkgs/main/win-64::blas-1.0-mkl
intel-openmp pkgs/main/win-64::intel-openmp-2025.0.0-haa95532_1165
libhwloc pkgs/main/win-64::libhwloc-2.12.1-default_hfa10c62_1000
libiconv pkgs/main/win-64::libiconv-1.18-hc89ec93_0
libxml2 pkgs/main/win-64::libxml2-2.13.9-h6201b9f_0
mkl pkgs/main/win-64::mkl-2025.0.0-h585ebfc_931
mkl-service pkgs/main/win-64::mkl-service-2.5.2-py314h0b37514_0
mkl_fft pkgs/main/win-64::mkl_fft-2.2.0-py314h921b242_0
mkl_random pkgs/main/win-64::mkl_random-1.3.0-py314hdbada53_0
numpy pkgs/main/win-64::numpy-2.4.6-py314h650627e_0
numpy-base pkgs/main/win-64::numpy-base-2.4.6-py314h5fb32f3_0
tbb pkgs/main/win-64::tbb-2022.3.0-h90c84d6_0
tbb-devel pkgs/main/win-64::tbb-devel-2022.3.0-h90c84d6_0
Downloading and Extracting Packages: ...working... done
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Note: you may need to restart the kernel to use updated packages.
==> WARNING: A newer version of conda exists. <==
current version: 26.3.2
latest version: 26.5.3
Please update conda by running
$ conda update -n base -c defaults conda
%conda update numpy
Jupyter detected... 3 channel Terms of Service accepted Channels: - defaults Platform: win-64 Collecting package metadata (repodata.json): done Solving environment: done # All requested packages already installed. Note: you may need to restart the kernel to use updated packages.
==> WARNING: A newer version of conda exists. <==
current version: 26.3.2
latest version: 26.5.3
Please update conda by running
$ conda update -n base -c defaults conda
%conda remove numpy
Jupyter detected...
3 channel Terms of Service accepted
Channels:
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: C:\Users\schol\miniconda3\envs\jn
removed specs:
- numpy
The following packages will be REMOVED:
blas-1.0-mkl
intel-openmp-2025.0.0-haa95532_1165
libhwloc-2.12.1-default_hfa10c62_1000
libiconv-1.18-hc89ec93_0
libxml2-2.13.9-h6201b9f_0
mkl-2025.0.0-h585ebfc_931
mkl-service-2.5.2-py314h0b37514_0
mkl_fft-2.2.0-py314h921b242_0
mkl_random-1.3.0-py314hdbada53_0
numpy-2.4.6-py314h650627e_0
numpy-base-2.4.6-py314h5fb32f3_0
tbb-2022.3.0-h90c84d6_0
tbb-devel-2022.3.0-h90c84d6_0
Downloading and Extracting Packages: ...working... done
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Note: you may need to restart the kernel to use updated packages.
==> WARNING: A newer version of conda exists. <==
current version: 26.3.2
latest version: 26.5.3
Please update conda by running
$ conda update -n base -c defaults conda
5. %%file¶
In Jupyter Notebook/IPython, %%file is a cell magic used to write the contents of a cell to a file.
Similar to %%writefile.
Useful for creating scripts, configuration files, or text files directly from a notebook.
May require an extension depending on the environment.
%%file hello.txt
Hello World
This is a text file.
Writing hello.txt
6. %%js¶
In Jupyter Notebook/IPython, %%js is a cell magic used to execute JavaScript code in the notebook frontend.
Allows interaction with notebook elements through JavaScript.
Useful for customizing notebook behavior and visualization.
%%js
alert("Hello from JavaScript!");
7. %%prun¶
In Jupyter Notebook/IPython, %%prun is a cell magic used to profile Python code.
Uses Python's built-in cProfile module.
Helps identify performance bottlenecks in code.
%%prun
sum(range(100000))
4 function calls in 0.004 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.003 0.003 0.003 0.003 {built-in method builtins.sum}
1 0.000 0.000 0.003 0.003 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.003 0.003 <string>:1(<module>)
8. %%python¶
In Jupyter Notebook/IPython, %%python is a cell magic used to execute Python code in a separate Python process.
Useful when working with multiple language kernels.
Requires the appropriate extension or environment support.
%%python
print("Hello from Python")
Hello from Python
Comments
Post a Comment