[IPython-User] Kernprof in ipython 0.11?

Robert Kern robert.kern@gmail....
Mon Apr 2 04:17:14 CDT 2012


On 4/1/12 2:58 AM, Ariel Rokem wrote:
> Hi Robert and all,
>
> On Wed, Sep 21, 2011 at 4:06 PM, Robert Kern<robert.kern@gmail.com>  wrote:
>> On 9/20/11 9:08 AM, Johann Cohen-Tanugi wrote:
>>> Hi Robert,
>>>
>>> On 09/02/2011 04:08 AM, Robert Kern wrote:
>>>> On 9/1/11 7:07 AM, reckoner wrote:
>>>>> Will the exceptional kernprof module make it into IPython 0.11 as a magic?
>>>> Not distributed *with* IPython, no. It's easy to configure IPython 0.11 to add
>>>> its %lprun magic, though. Just make a file line_profiler_ext.py in your
>>>> ~/.ipython/extensions/ directory:
>>>>
>>>>       import line_profiler
>>>>       def load_ipython_extension(ip):
>>>>           ip.define_magic('lprun', line_profiler.magic_lprun)
>>>>
>>>> Then add it to your list of extensions in your ipython_config.py:
>>>>
>>>> # A list of dotted module names of IPython extensions to load.
>>>> c.TerminalIPythonApp.extensions = [
>>>>         'line_profiler_ext',
>>>> ]
>>>>
>>> I just tried that, and I get :
>>> In [1]: %lprun -f polylog.polylog(2,0.5)
>>> ---------------------------------------------------------------------------
>>> ImportError                               Traceback (most recent call last)
>>> /home/cohen/<ipython-input-1-4a7604b1bbac>    in<module>()
>>> ---->    1 get_ipython().magic(u"lprun -f polylog.polylog(2,0.5)")
>>>
>>> /usr/local/lib/python2.6/dist-packages/ipython-0.11-py2.6.egg/IPython/core/interactiveshell.pyc
>>> in magic(self, arg_s, next_input)
>>>       1892                 self._magic_locals = sys._getframe(1).f_locals
>>>       1893             with self.builtin_trap:
>>> ->    1894                 result = fn(magic_args)
>>>       1895             # Ensure we're not keeping object references around:
>>>
>>>       1896             self._magic_locals = {}
>>>
>>> /usr/local/lib/python2.6/dist-packages/line_profiler-1.0b2-py2.6-linux-i686.egg/line_profiler.pyc
>>> in magic_lprun(self, parameter_s)
>>>        166     """
>>>        167     # Local import to avoid hard dependency.
>>>
>>> -->    168     from IPython.genutils import page
>>>        169     from IPython.ipstruct import Struct
>>>        170     from IPython.ipapi import UsageError
>>>
>>> ImportError: No module named genutils
>>
>> Oh, I guess I need to update the imports to match the refactoring, then.
>> Bother.
>>
>
> Does anyone know whether this works with 0.12 and with the current
> config layout? If anyone has done this successfully, where did you put
> the aforementioned line_profiler_ext.py file?

The current hg tip and the 1.0b3 release work with IPython 0.12.

   https://bitbucket.org/robertkern/line_profiler/overview

Just add 'line_profiler' to your c.TerminalIPythonApp.extensions list. You don't 
need to make a separate line_profiler_ext.py file anymore. E.g., from my 
~/.ipython/profile_default/ipython_config.py file:

# A list of dotted module names of IPython extensions to load.
c.TerminalIPythonApp.extensions = [
     'kernmagic',
     'line_profiler',
     'see_ext',
]

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-User mailing list