我试图使用line_profiler
模块分析一些Python代码,但我无法让它工作。我在Windows 7上使用Python 2.7.6。
在以下kernprof -l -v test.py
文件上运行test.py
时:
@profile
def test():
a = 1
b = 1
return a + b
if __name__ == '__main__':
print test()
我明白了:
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
显然,如果我注释掉包含line_profiler
的@profile装饰器的行,代码将运行良好。我在做什么错了,在这里?
答案 0 :(得分:2)
答案 1 :(得分:0)
from memory_profiler import profile
@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
或:
python -m memory_profiler example.py
答案 2 :(得分:-1)
这是networkx
包1.11中的错误的结果。像这样降级:
pip uninstall networkx
pip install networkx==1.7