IPython Magic Commands in Jupyter Notebook (2026) - Part 3 | 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. %env¶
In Jupyter Notebook, %env is a magic command used to view or set environment variables.
Environment variables store system-related configuration values.
It helps users manage paths and settings inside the notebook.
%env
{'ALLUSERSPROFILE': 'C:\\ProgramData',
'APPDATA': 'C:\\Users\\schol\\AppData\\Roaming',
'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files',
'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files',
'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files',
'COMPUTERNAME': 'STFBONC',
'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe',
'CONDA_PREFIX': 'C:\\Users\\schol\\miniconda3\\envs\\jn',
'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData',
'EFC_15260_1262719628': '1',
'EFC_15260_1592913036': '1',
'EFC_15260_2283032206': '1',
'EFC_15260_2775293581': '1',
'EFC_15260_3789132940': '1',
'EFC_15260_4126798990': '1',
'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer',
'FPS_BROWSER_USER_PROFILE_STRING': 'Default',
'HOMEDRIVE': 'C:',
'HOMEPATH': '\\Users\\schol',
'IPY_INTERRUPT_EVENT': '4920',
'JPY_INTERRUPT_EVENT': '4920',
'JPY_PARENT_PID': '5036',
'JPY_SESSION_NAME': 'C:\\Users\\schol\\Projects\\Magic-Commands\\Magic-Commands-3.ipynb',
'LOCALAPPDATA': 'C:\\Users\\schol\\AppData\\Local',
'LOGONSERVER': '\\\\STFBONC',
'NUMBER_OF_PROCESSORS': '4',
'ONEDRIVE': 'C:\\Users\\schol\\OneDrive',
'OS': 'Windows_NT',
'PATH': 'C:\\Users\\schol\\miniconda3\\envs\\jn;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\mingw-w64\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\usr\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Scripts;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\schol\\AppData\\Local\\Microsoft\\WindowsApps',
'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
'PROCESSOR_ARCHITECTURE': 'AMD64',
'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 142 Stepping 12, GenuineIntel',
'PROCESSOR_LEVEL': '6',
'PROCESSOR_REVISION': '8e0c',
'PROGRAMDATA': 'C:\\ProgramData',
'PROGRAMFILES': 'C:\\Program Files',
'PROGRAMFILES(X86)': 'C:\\Program Files (x86)',
'PROGRAMW6432': 'C:\\Program Files',
'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules',
'PUBLIC': 'C:\\Users\\Public',
'SESSIONNAME': 'Console',
'SYSTEMDRIVE': 'C:',
'SYSTEMROOT': 'C:\\WINDOWS',
'TEMP': 'C:\\Users\\schol\\AppData\\Local\\Temp',
'TMP': 'C:\\Users\\schol\\AppData\\Local\\Temp',
'USERDOMAIN': 'STFBONC',
'USERDOMAIN_ROAMINGPROFILE': 'STFBONC',
'USERNAME': 'schol',
'USERPROFILE': 'C:\\Users\\schol',
'WINDIR': 'C:\\WINDOWS',
'ZES_ENABLE_SYSMAN': '1',
'PYDEVD_USE_FRAME_EVAL': 'NO',
'TERM': 'xterm-color',
'CLICOLOR': '1',
'FORCE_COLOR': '1',
'CLICOLOR_FORCE': '1',
'PAGER': 'cat',
'GIT_PAGER': 'cat',
'MPLBACKEND': 'module://matplotlib_inline.backend_inline'}
%env APPDATA
'C:\\Users\\schol\\AppData\\Roaming'
2. %pycat¶
In Jupyter Notebook, %pycat is a magic command used to display the contents of a Python file with syntax highlighting.
It improves readability of source code.
It is useful for viewing scripts directly inside the notebook.
%pycat script.py
print("Hello from script file")
3. %load¶
In Jupyter Notebook, %load is a magic command used to load code from an external file into a notebook cell.
It helps reuse existing Python scripts.
It is useful for editing and testing external code.
# %load script.py
print("Hello from script file")
4. %save¶
In Jupyter Notebook, %save is a magic command used to save commands or code from notebook history into a Python file.
It helps export notebook code for later use.
It is useful for creating reusable scripts.
%save myscript.py 1-5
The following commands were written to file `myscript.py`:
get_ipython().run_line_magic('env', '')
get_ipython().run_line_magic('env', 'APPDATA')
get_ipython().run_line_magic('pycat', 'script.py')
get_ipython().run_line_magic('load', 'script.py')
get_ipython().run_line_magic('save', 'myscript.py 1-5')
5. %store¶
In Jupyter Notebook, %store is a magic command used to store variables permanently between notebook sessions.
It allows saved variables to be reused later.
It is useful for preserving important data.
data=3
%store data
Stored 'data' (int)
%store
Stored variables and their in-db values: data -> 3
6. %alias¶
In Jupyter Notebook, %alias is a magic command used to create shortcuts for system commands.
It helps simplify frequently used commands.
It improves command-line productivity.
%alias ll dir
ll
Volume in drive C is Windows-SSD
Volume Serial Number is 565A-BBE5
Directory of C:\Users\schol\Projects\Magic-Commands
05/29/2026 02:13 <DIR> .
05/25/2026 16:01 <DIR> ..
05/28/2026 01:28 <DIR> .ipynb_checkpoints
05/26/2026 17:07 39,584 Magic-Commands-1.ipynb
05/27/2026 15:41 171,122 Magic-Commands-2.ipynb
05/29/2026 02:13 18,695 Magic-Commands-3.ipynb
05/25/2026 02:58 10,184 Magic-Commands-4.ipynb
05/26/2026 15:46 8,905 Magic-Commands-5.ipynb
05/29/2026 02:14 266 myscript.py
05/27/2026 01:16 33 script.py
05/26/2026 17:07 <DIR> Test
7 File(s) 248,789 bytes
4 Dir(s) 137,213,587,456 bytes free
7. %unalias¶
In Jupyter Notebook, %unalias is a magic command used to remove an existing alias.
It deletes previously created command shortcuts.
It helps manage custom aliases.
%unalias ll
ll
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 1 ----> 1 ll NameError: name 'll' is not defined
8. %debug¶
In Jupyter Notebook, %debug is a magic command used to start the interactive debugger after an error occurs.
It helps inspect variables and trace errors.
It is useful for debugging Python programs.
a = 10
b = 0
c = a / b # ZeroDivisionError
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[12], line 3 1 a = 10 2 b = 0 ----> 3 c = a / b # ZeroDivisionError ZeroDivisionError: division by zero
%debug
> c:\users\schol\appdata\local\temp\ipykernel_14968\900425174.py(3)<module>()
10
0
*** ZeroDivisionError: division by zero
9. %precision¶
In Jupyter Notebook, %precision is a magic command used to control the display precision of floating-point numbers.
It changes how decimal values are shown.
It is useful for formatting numerical output.
1/3
0.3333333333333333
%precision 3
'%.3f'
1/3
0.333
10. %system¶
In Jupyter Notebook, %system is a magic command used to run operating system commands from the notebook.
It allows interaction with the system shell.
It is useful for file and system operations.
%system dir
[' Volume in drive C is Windows-SSD', ' Volume Serial Number is 565A-BBE5', '', ' Directory of C:\\Users\\schol\\Projects\\Magic-Commands', '', '05/29/2026 02:13 <DIR> .', '05/25/2026 16:01 <DIR> ..', '05/28/2026 01:28 <DIR> .ipynb_checkpoints', '05/26/2026 17:07 39,584 Magic-Commands-1.ipynb', '05/27/2026 15:41 171,122 Magic-Commands-2.ipynb', '05/29/2026 02:13 18,695 Magic-Commands-3.ipynb', '05/25/2026 02:58 10,184 Magic-Commands-4.ipynb', '05/26/2026 15:46 8,905 Magic-Commands-5.ipynb', '05/29/2026 02:14 266 myscript.py', '05/27/2026 01:16 33 script.py', '05/26/2026 17:07 <DIR> Test', ' 7 File(s) 248,789 bytes', ' 4 Dir(s) 137,214,869,504 bytes free']
Comments
Post a Comment