Useful IPython magic commands
Last Updated :
06 Oct, 2023
In this article, we will cover IPython Magic commands/functions and discuss various types of magic commands available. In this article we will be using Jupyter Notebook to execute the magic commands first, we look at what are Magic functions and why we use them, then different types of magic functions followed by examples. There are a lot of magic functions but in this article, we discuss the most commonly used magic functions.
Jupyter Notebook
The Jupyter Notebook is the original web application for creating and sharing computational documents that contain live code, equations, visualizations, and narrative text. It offers a simple, streamlined, document-centric experience. Jupyter has support for over 40 different programming languages and Python is one of them.
Magic Commands
Magic commands generally known as magic functions are special commands in IPython that provide special functionalities to users like modifying the behavior of a code cell explicitly, simplifying common tasks like timing code execution, profiling, etc. Magic commands have the prefix '%' or '%%' followed by the command name. There are two types of magic commands:
- Line Magic Commands
- Cell Magic Commands
Line Magic Commands
Line magic commands are used to provide a special functionality to a single line of code, line magic commands begin with '%' followed by the line command. To check the available magic commands run '%lsmagic' this command will show all the magic commands, as of now there are 97 line magic commands which are as follows:
%alias, %alias_magic, %autoawait, %autocall, %automagic, %autosave, %bookmark, %cat, %cd, %clear, %colors, %conda, %config, %connect_info, %cp, %debug, %dhist, %dirs, %doctest_mode, %ed, %edit, %env, %gui, %hist, %history, %killbgscripts, %ldir, %less, %lf, %lk, %ll, %load, %load_ext, %loadpy, %logoff, %logon, %logstart, %logstate, %logstop, %ls, %lsmagic, %lx, %macro, %magic, %man, %matplotlib, %mkdir, %more, %mv, %notebook, %page, %pastebin, %pdb, %pdef, %pdoc, %pfile, %pinfo, %pinfo2, %pip, %popd, %pprint, %precision, %prun, %psearch, %psource, %pushd, %pwd, %pycat, %pylab, %qtconsole, %quickref, %recall, %rehashx, %reload_ext, %rep, %rerun, %reset, %reset_selective, %rm, %rmdir, %run, %save, %sc, %set_env, %store, %sx, %system, %tb, %time, %timeit, %unalias, %unload_ext, %who, %who_ls, %whos, %xdel, %xmode
Since it is not possible to cover all the 97 line magic commands we will only cover the most widely and helpful commands in this article, we will look some line magic commands along with the short description followed by example of some line magic commands:
|
%alias
| Define an alias for a system command.
|
%alias_magic
| Create an alias for an existing line or cell magic
|
%automagic
| Make magic functions callable without having to type the initial %
|
%cd
| Change the current working directory
|
%debug
| Activate the interactive debugger
|
%dhist
| Print your history of visited directories
|
%dirs
| Return the current directory stack
|
%env
| Get, set, or list environment variables.
|
%gui
| Enable or disable IPython GUI event loop integration
|
%load
| Load code into the current frontend
|
%load_ext
| Load an IPython extension by its module name
|
%lsmagic
| List All Available Magic Commands
|
%macro
| Define a macro for future re-execution. It accepts ranges of history, filenames or string objects
|
%magic
| Print Information About Magic Commands System
|
%matplotlib
| Set up matplotlib to work interactively.
|
%notebook
| Export and convert IPython notebooks
|
%pfile
| Print (or run through pager) the file where an object is defined
|
%pip
| Run the pip package manager within the current kernel
|
%prun
| Run a statement through the python code profiler
|
%pwd
| Return the current working directory path
|
%reload_ext
| Reload an IPython extension by its module name
|
%reset
| Resets the namespace by removing all names defined by the user
|
%run
| Run the named file inside IPython as a program
|
%save
| Save a set of lines or a macro to a given filename
|
%set_env
| Set environment variables
|
%system
| Shell execute - run shell command and capture output
|
%time
| Measures time execution of a Python statement or expression
|
%timeit
| Measures Execution Time of Line
|
%unalias
| Remove an alias
|
%who
| print all interactive variables, with some minimal formatting
|
%who_ls
| Return a sorted list of all interactive variables
|
%whos
| Like %who, but gives some extra information about each variable
|
%xdel
| Delete a variable, trying to clear it from anywhere that IPython’s machinery has references to it.
|
%xmode
| Switch modes for the exception handlers
|
Example of Line Magic Functions
1. %load
- Load the code from external file and insert in into the cell
- Syntax:
%load file_name.py
Example:
Python3
2. %run
- Run the named file inside IPython as a program
- Syntax:
%run file_name.py
Example:
Python3
Output:

3. %time
- Measure the execution time of a single Python statement or expression
- Syntax:
%time some_funtion()
Example:
Python3
def fibonacci(n):
if n == 0 or n == 1: return n
return fibonacci(n-1)+fibonacci(n-2)
%time fibonacci(10)
Output:
CPU times: total: user 20 ns, sys:4 ns, total: 24 ns
Wall time: 26.2 ns
55
4. %timeit
- Measure the execution time of a single Python statement or expression by performing executions multiple time to get more accurate results.
- Syntax:
%timeit some_function()
Example:
Python3
def fibonacci(n):
if n == 0 or n == 1: return n
return fibonacci(n-1)+fibonacci(n-2)
%timeit fibonacci(10)
Output:
14.7 µs ± 869 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
5. %cd
- Change the current working directory
- Syntax:
%cd directory/sub_dir
Example:
Python3
%cd Form_data_visualization
Output:

Cell Magic Commands
Cell Magic functions are special commands that allow the user to modify the behavior of a code cell explicitly. Cell Magic functions have the prefix '%%' followed by the command name. Cell magic functions serve various tasks and customizations which we discuss thoroughly further in this article. As per the official python documentation there are 16 cell magic functions available now which are described below,
|
%%bash
| Run cells with bash in a subprocess
|
%%capture
| run the cell, capturing stdout, stderr, and IPython’s rich display() calls
|
%%html
| Render the cell as a block of HTML
|
%%javascript or %%js
| Run the cell block of Javascript code
|
%%latex
| Render the cell as a block of LaTeX
|
%%markdown
| Render the cell as Markdown text block
|
%%perl
| Run cells with perl in a subprocess
|
%%pypy
| Run cells with pypy in a subprocess
|
%%python
| Run cells with python in a subprocess
|
%%python2
| Run cells with python2 in a subprocess
|
%%python3
| Run cells with python3 in a subprocess
|
%%ruby
| Run cells with ruby in a subprocess
|
%%script
| Run a cell via a shell command
|
%%sh
| Run cells with sh in a subprocess
|
%%svg
| Render the cell as an SVG literal
|
%%writefile
| Write the contents of the cell to a file
|
Since, its not possible to explain each cell magic command with example we will only cover some helpful and widely used cell magic commands.
Example of Cell Magic Functions
1. %%bash
Allows user to run Bash shell commands within the cell.
Example:
Python3
Output Screenshot:

2. %%html
This cell magic function renders contents of code cell as html script
Example:
Python3
%%html
<font size=10 color='hotpink'>Hello World</font>
Output Screenshot:

3. %%javascript or %%js
Allows you to execute JavaScript code within the cell
Example:
Python3
%%javascript
alert("This is a JavaScript alert!")
Output Screenshot:

4. %%latex
Renders LaTeX equations and expressions in the cell.
Python3
%%latex
$e^{i\pi} + 1 = 0$
Output Screenshot:

5. %%markdown or %%md
Renders the content of the cell as Markdown
Example:
Python3
%%markdown
# This is a Markdown Heading
* This is a bullet point
Output Screenshot:

Similar Reads
Executing Shell Commands with Python This article starts with a basic introduction to Python shell commands and why one should use them. It also describes the three primary ways to run Python shell commands.os.system()subprocess.run()subprocess.Popen()Â What is a shell in the os?In programming, the shell is a software interface for acce
4 min read
Magic Commands for Profiling in Jupyter Notebook Jupyter Notebook is a versatile tool or IDE widely used mostly by data scientists, researchers, and programmers for interactive computing and data analysis, dashboards, and visualizations. It offers a unique and rich set of features. Some topics are there it can perform such as: running the code in
9 min read
Run shell command from GUI using Python In this article, we are going to discuss how we can create a GUI window that will take Operating System commands as input and after executing them, displays the output in a pop-up window. Modules Required: PyAutoGui: It is a module in python which is used to automate the GUI and controls the mouse a
2 min read
Python A-Z Quick Notes Python is a general-purpose, high-level, interpreted programming language that supports multiple programming paradigms, including procedural, object-oriented, and functional programming, and is widely used among the developersâ community. Python was mainly developed for emphasis on code readability,
15+ min read
Command Line Scripts | Python Packaging How do we execute any script in Python? $ python do_something.py $ python do_something_with_args.py gfg vibhu Probably that's how you do it. If your answer was that you just click a button on your IDE to execute your Python code, just assume you were asked specifically how you do it on command line.
4 min read
Python vs Cpython Python is a high-level, interpreted programming language favored for its readability and versatility. It's widely used in web development, data science, machine learning, scripting, and more. However, Cpython is the default and most widely used implementation of the Python language. It's written in
4 min read
Useful Pycharm Keyboard Shortcuts Pycharm is a popular Integrated Development Environment (IDE) for Python. It was developed by JetBrains. It provides a wide range of features to enhance productivity. It provides code analysis, integrated unit testing, a debugger, integration with version control, and also supports web development i
3 min read
Top Django Commands Mastering these Django commands will enhance your development workflow, streamline common tasks, and help you manage your Django projects more effectively. By becoming familiar with these commands, you'll be better equipped to handle various aspects of your Django applications, from initial setup to
6 min read
Python | os.system() method The OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system-dependent functionality.os.system()Â method executes the command (a string) in a subshell. This method
3 min read
Keywords in Python | Set 2 Python Keywords - Introduction Keywords in Python | Set 1 More keywords:16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in "try" block is checked, if there is any type of error, except block is executed. 17. except : As expl
4 min read