简体   繁体   中英

Python profiling: using line_profiler's @profile decorator results in error

I'm trying to profile some Python code using the line_profiler module, but I can't get it to work. I'm on Windows 7 and using Python 2.7.6.

When running kernprof -l -v test.py on the following test.py file:

@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 run fine if I comment out the line containing line_profiler 's @profile decorator. What am I doing wrong, here?

Might be related to python 2 and futures, see this bug report, this pull request fixes this issue. Before a new version is uploaded to pypi, you can modify the code accordingly to that commit .

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/

That's result of a bug in networkx package 1.11. Downgrade like so:

pip uninstall networkx
pip install networkx==1.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM