🌐
IPython
ipython.readthedocs.io › en › stable › interactive › magics.html
Built-in magic commands — IPython 9.13.0 documentation
February 2, 2026 - By default IPython’s cd command does print this directory, since the default prompts do not display path information. ... Note that !cd doesn’t work for this purpose because the shell where !command runs is immediately discarded after executing ‘command’. ... Simple magic to quickly define a code transformer for all IPython’s future input.
🌐
TutorialsPoint
tutorialspoint.com › jupyter › ipython_magic_commands.htm
IPython - Magic Commands
Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 01.03-magic-commands.html
IPython Magic Commands | Python Data Science Handbook
The previous two sections showed how IPython lets you use and explore Python efficiently and interactively. Here we'll begin discussing some of the enhancements that IPython adds on top of the normal Python syntax. These are known in IPython as magic commands, and are prefixed by the % character.
🌐
IPython
ipython.org › ipython-doc › 3 › config › custommagics.html
Defining custom magics — IPython 3.2.1 documentation
This documentation is for an old version of IPython. You can find docs for newer versions here. There are two main ways to define your own magic functions: from standalone functions and by inheriting from a base class provided by IPython: IPython.core.magic.Magics.
🌐
IPython
ipython.readthedocs.io › en › 9.2.0 › interactive › magics.html
Built-in magic commands — IPython 9.2.0 documentation
Parameters passed to the magic function. ... Allow to change the status of the autoawait option. This allow you to set a specific asynchronous code runner. If no value is passed, print the currently used asynchronous integration and whether it is activated. It can take a number of value evaluated in the following order: ... sync turn on the pseudo-sync integration (mostly used for IPython.embed() which does not run IPython with a real eventloop and deactivate running asynchronous code.
🌐
Ipython-books
ipython-books.github.io › 14-creating-an-ipython-extension-with-custom-magic-commands
IPython Cookbook - 1.4. Creating an IPython extension with custom magic commands
Here, loading our extension immediately registers our %%csv magic function in the interactive shell: ... An IPython extension is a Python module that implements the top-level load_ipython_extension(ipython) function. When the %load_ext magic command is called, the module is loaded and the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › useful-ipython-magic-commands
Useful IPython magic commands - GeeksforGeeks
July 23, 2025 - 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...
🌐
Python for Data Science
python4data.science › en › 24.3.0 › workspace › ipython › magics.html
IPython magic - Python for Data Science 24.3.0
IPython not only enables Python to be used interactively, but also extends the Python syntax with so-called magic commands, which are provided with the prefix %. They are designed to quickly and easily solve common data analysis problems.
Find elsewhere
🌐
IPython
ipython.org › ipython-doc › 3 › parallel › magics.html
Parallel Magic Commands — IPython 3.2.1 documentation
The initial magics registered when you create a client are the result of a call to rc.activate() with default args. Engines are really the same object as the Kernels used elsewhere in IPython, with the minor exception that engines connect to a controller, while regular kernels bind their sockets, ...
🌐
IPython
ipython.readthedocs.io › en › stable › config › custommagics.html
Defining custom magics — IPython 9.12.0 documentation
There are two main ways to define your own magic functions: from standalone functions and by inheriting from a base class provided by IPython: IPython.core.magic.Magics.
🌐
Switowski
switowski.com › blog › creating-magic-functions-part1
Creating Magic Functions in IPython - Part 1
Next, we will create a %pn magic function that will use the above code to interpret Polish notation. from collections import deque from IPython.core.magic import register_line_magic @register_line_magic def pn(line): """Polish Notation interpreter Usage: >>> %pn + 2 2 4 """ return interpret(deque(line.split()))
🌐
JetBrains
jetbrains.com › help › pycharm › ipython.html
IPython magic commands | PyCharm Documentation
In the main menu, go to Tools | Python Console. If IPython has been properly installed, PyCharm will report about the version used. In the lower part of the console, start typing the magic commands, and press Enter to execute them.
Top answer
1 of 5
96

It depends on which version of IPython you have. If you have 1.x:

from IPython import get_ipython
ipython = get_ipython()

If you have an older version:

import IPython.core.ipapi  
ipython = IPython.core.ipapi.get()

or

import IPython.ipapi  
ipython = IPython.ipapi.get()

Once that's done, run a magic command like this:

ipython.magic("timeit abs(-42)")

Note: The script must be run via ipython.

See Line magics

magic(...) is deprecated since IPython 0.13, use run_line_magic(magic_name, parameter_s)

ipython.run_line_magic("timeit", "abs(-42)")
run_line_magic(magic_name: str, line, _stack_depth=1) method of ipykernel.zmqshell.ZMQInteractiveShell instance
    Execute the given line magic.
    
    Parameters
    ----------
    magic_name : str
        Name of the desired magic function, without '%' prefix.
    line : str
        The rest of the input line as a single string.
    _stack_depth : int
        If run_line_magic() is called from magic() then _stack_depth=2.
        This is added to ensure backward compatibility for use of 'get_ipython().magic()'

Also see ipython.run_cell_magic and Cell magics

run_cell_magic(magic_name, line, cell) method of ipykernel.zmqshell.ZMQInteractiveShell instance
    Execute the given cell magic.
    
    Parameters
    ----------
    magic_name : str
        Name of the desired magic function, without '%' prefix.
    line : str
        The rest of the first input line as a single string.
    cell : str
        The body of the cell as a (possibly multiline) string.
2 of 5
6

Both IPython and the timeit module, when called with python -m timeit, execute the same loop with a growing value of number until the timing result surpasses a certain threshold that guarantees the time measurement is mostly free of operating system interferences.

You can compare the IPython implementation of the %timeit magic with the Python timeit standard module to see that they are doing mostly the same.

So, answering your question, you should probably replicate the same loop until you find the correct value for the number parameter.

🌐
Reddit
reddit.com › r/learnpython › what exactly are magic commands?
r/learnpython on Reddit: What exactly are magic commands?
January 29, 2024 -

Hello all, just had a quick question on what exactly is a magic command?

I am working in a 3P environment where I have no control over libraries/modules installed, but help desk (for a lack of a better term) let me know that I can use the magic command as a "work around"? I.e. %install xlsxwriter?

What is the point of this, and what does it do exactly? My uneducated guess is that it installs a module each time you run the script?

Any help would be greatly appreciated. Thank you all!!

🌐
IPython
ipython.readthedocs.io › en › 8.31.0 › interactive › magics.html
Built-in magic commands — IPython 8.31.0 documentation
Note that magic functions have lowest priority, so if there’s a variable whose name collides with that of a magic fn, automagic won’t work for that function (you get the variable instead). However, if you delete the variable (del var), the previously shadowed magic function becomes visible to automagic again. ... Manage IPython’s bookmark system.
🌐
Python for Data Science
python4data.science › en › latest › workspace › ipython › magics.html
IPython magic - Python for Data Science
IPython not only enables Python to be used interactively, but also extends the Python syntax with so-called magic commands, which are provided with the prefix %. They are designed to quickly and easily solve common data analysis problems.
🌐
IPython
ipython.readthedocs.io › en › 9.0.2 › interactive › magics.html
Built-in magic commands — IPython 9.0.2 documentation
March 8, 2025 - Parameters passed to the magic function. ... Allow to change the status of the autoawait option. This allow you to set a specific asynchronous code runner. If no value is passed, print the currently used asynchronous integration and whether it is activated. It can take a number of value evaluated in the following order: ... sync turn on the pseudo-sync integration (mostly used for IPython.embed() which does not run IPython with a real eventloop and deactivate running asynchronous code.
🌐
GitHub
github.com › ipython › ipython › blob › main › IPython › core › magic.py
ipython/IPython/core/magic.py at main · ipython/ipython
Official repository for IPython itself. Other repos in the IPython organization contain things like the website, documentation builds, etc. - ipython/IPython/core/magic.py at main · ipython/ipython
Author   ipython