python cProfile 抛出 'int' 对象不可调用错误
以下脚本运行得很好:
$ python myscript.py
当我尝试使用 cProfile 来分析我的代码时,比如:
$ python -m cProfile -s time myscript.py
或者
$ python -m cProfile myscript.py
我遇到了以下错误:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 121, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 34, in _run_code
exec code in run_globals
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 190, in <module>
main()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 183, in main
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 36, in run
result = prof.print_stats(sort)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 81, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pstats.py", line 92, in __init__
self.init(arg)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pstats.py", line 106, in init
self.load_stats(arg)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pstats.py", line 130, in load_stats
arg.create_stats()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 92, in create_stats
self.snapshot_stats()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/cProfile.py", line 106, in snapshot_stats
callersdicts[id(entry.code)] = callers
TypeError: 'int' object is not callable
我的脚本在这两种情况下都能成功运行,只是在后者的情况下让 cProfile 卡住了。我知道这一定是个小问题,但就是找不到原因。
请帮我解决这个问题。谢谢!
1 个回答
6
你有一个整数变量叫做 id
,这个名字和Python内置的一个函数 id
重名了。这样会导致 cProfile
出现问题。
只需要把你的 id
变量改个名字,cProfile
就能正常工作了。