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

后端 未结 2 1226
情深已故
情深已故 2021-01-31 08:16

Problem

Most iPython \"magic functions\" work fine for me right off the bat: %hist, %time, %prun, etc. However, I noticed that

相关标签:
2条回答
  • 2021-01-31 08:50

    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
    
    0 讨论(0)
  • 2021-01-31 09:01

    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.

    0 讨论(0)
提交回复
热议问题