gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:40:17 25 4
gpt4 key购买 nike

我正在尝试使用 line_profiler 模块分析一些 Python 代码,但我无法让它工作。我在 Windows 7 上使用 Python 2.7.6。

在以下 test.py 文件上运行 kernprof -l -v 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 装饰器的行,代码将运行良好。我在这里做错了什么?

最佳答案

可能与 python 2 和 futures 有关,参见 this错误报告,this拉取请求修复了这个问题。在将新版本上传到 pypi 之前,您可以根据该 commit 修改代码。 .

关于Python 分析 : using line_profiler's @profile decorator results in error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com