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
Using the Cat Command in Python
The cat command is a Linux shell command. It is the shorthand for concatenate. It is placed among the most often used shell commands. It could be used for various purposes such as displaying the content of a file on the terminal, copying the contents of a given file to another given file, and both a
4 min read
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 acc
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
What Can I Do With Python?
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Let's see what Python programming does: Uses of PythonIn terms of
5 min read
Python Course Syllabus
Hereâs a straight-to-the-point breakdown of what a python course covers from the basics to advanced concepts like data handling, automation and object-oriented programming. No fluff, just the essentials to get you coding fast. Getting Started with Python ProgrammingWelcome to the getting started wit
6 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
MATLAB Commands
MATLAB is an interactive multi-programming language and numeric computing environment developed by MathWorks. MATLAB provides the Commands that will be used when the user wants to interact with any application using the command line interface. Following are the lists of commands used in MATLAB. Comm
3 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
Python User defined functions
A User-Defined Function (UDF) is a function created by the user to perform specific tasks in a program. Unlike built-in functions provided by a programming language, UDFs allow for customization and code reusability, improving program structure and efficiency. Example: [GFGTABS] Python # function de
6 min read