Python profiling: using line_profiler @profile constructor results in an error

I am trying to profile Python code using a module line_profiler

, but I cannot get it to work. I am on Windows 7 and using Python 2.7.6.

When run kernprof -l -v test.py

in the following file test.py

:

@profile
def test():
    a = 1
    b = 1
    return a + b

if __name__ == '__main__':
    print test()

      

I get:

C:\>kernprof -l -v C:\test.py
Wrote profile results to test.py.lprof
Timer unit: 3.01262e-07 s

Traceback (most recent call last):
  File "C:\path_to_kernprof\kernprof-script.py", line 10, in <module>
    sys.exit(main())
  File "C:\path_to_kernprof\kernprof.py", line 221, in main
    execfile(script_file, ns, ns)
  File "C:\test.py", line 1, in <module>
    @profile
NameError: name 'profile' is not defined

      

Obviously, the code will work fine if I comment out the line containing line_profiler

@profile decorator. What am I doing wrong, here?

+3


source to share


3 answers


It might have something to do with python 2 and futures, see this bug report , this pull request fixes this issue. Before the new version is uploaded to pypi, you can change the code corresponding to this commit .



+2


source


from memory_profiler import profile

@profile

def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    del b
    return a

      

or:



python -m memory_profiler example.py

      

https://pypi.org/project/memory-profiler/

0


source


This bug results in package networkx

1.11. Lower it like this:

pip uninstall networkx
pip install networkx==1.7

      

-1


source







All Articles