Skip to content

Instantly share code, notes, and snippets.

@kylegibson
Last active June 27, 2022 14:34
Show Gist options
  • Save kylegibson/6583590 to your computer and use it in GitHub Desktop.
Save kylegibson/6583590 to your computer and use it in GitHub Desktop.
from decorator import decorator
from line_profiler import LineProfiler
@decorator
def profile_each_line(func, *args, **kwargs):
profiler = LineProfiler()
profiled_func = profiler(func)
try:
profiled_func(*args, **kwargs)
finally:
profiler.print_stats()
@saurabh-kataria
Copy link

I am getting this error: AttributeError: 'NoneType' object has no attribute '__code__'

def a():
    x=np.ones(100000)
    b()
def b():
    y=np.ones(20000)
profile_each_line(a())

@jthistle
Copy link

@saurabh-kataria: you need to write:

@profile_each_line
def a():
    x=np.ones(100000)
    b()

def b():
    y=np.ones(20000)

a() in your snippet returns None (which is what all Python functions return by default), which is why you're getting the error.

@Jaya472
Copy link

Jaya472 commented Jun 22, 2022

I am not able to see the Line Contents. Only the #hits, #time, etc are being shown. The output says

"Could not find file C:\Users\HP\AppData\Local\Temp\ipykernel_24440\670606222.py
Are you sure you are running this program from the same directory that you ran the profiler from?"

It used to work few months ago but suddenly now the above is happening. And I write my scripts in Onedrive with Anaconda Jupyter Lab. Please suggest what might be wrong. There is NO answer anywhere for this problem and I tried every possible way I could think of to solve it from 8 hours! Just to get the Line Contents so that I can begin my actual task of optimizing my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment