Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly python python

Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly


To make %lprun work, you need to load the extension into your session, using this command:

In [1]: %load_ext line_profiler

Check out this notebook to see some examples that use the magic.

Besides, if you are working with Spyder, there is also a third-party line_profiler plugin, which you can find here.


You have two ways to make work %lprun, one solution is temporal, i.e., it lasts until you finish your ipython session, and the other one is permanent.

Temporal: (as Carlos Cordoba's answer)

After starting ipython run the following:

In [1]: %load_ext line_profiler

Permanent:

Add the following lines to ~/.ipython/profile_default/ipython_config.py:

c.TerminalIPythonApp.extensions = [    'line_profiler',]

If you don't have the file ~/.ipython/profile_default/ipython_config.py, you can create it by (see this for more info):

ipython profile create