Search code examples
debuggingjupyter-notebookprofilingjupyter-labprofiler

Why Name not defined error with %lprun profiling? Suddenly not working


Previously, I had successfully used the %lprun profiler for my code in Jupyter notebook and now I want to profile some other code in JupyterLab. I used the same format as before but it is throwing a name error suddenly now. The simplified structure of my code is as below.

    %reload_ext line_profiler   #in a separate cell

    def fn1(x,y):
       z=x+y
       return z
    
    def fn2(u,v):
       x=u+v
       y= u*v
       %lprun -f fn1 z=fn1(x,y) #this format worked for me months ago but not now!

    fn2(100,200)   #calling the fn2 outside

I get the error as name 'x' is not defined. But If I remove the %lprun -f then the code works normally since I obviously defined x=u+v before calling fn1. Please anyone suggest what is wrong here. I am using Python 3 (ipykernel).


Solution

  • I figured it out on my own. I was supposed to call lprun from outside i.e. %lprun -f fn1 -fn2 fn2(100,200) and not inside fn2. Inside fn2, it will just be normal z=fn1(x,y) i.e. without any lprun.