3

Following script runs great:

$ python myscript.py 

When I try to profile my code using cProfile like:

$ python -m cProfile -s time myscript.py

or

$ python -m cProfile myscript.py

I get following error:

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

My script runs successfully in both the cases except that it chokes cProfile in latter case. I know it has to be something really petty, just cant nail it.

Please help me resolve. Thanks

jerrymouse
  • 16,964
  • 16
  • 76
  • 97
  • I tried googling, closest link I could get is http://infix.se/2009/09/03/profile-this It states same error, but doesnt mention any solution. – jerrymouse Sep 20 '11 at 18:43

1 Answers1

6

You have an integer variable named id that is masking the built-in function id. That is messing up cProfile.

Rename your id variable and cProfile should work fine.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Absolutely, its `id` that was messing up things. My bad, I realise. Thanks again – jerrymouse Sep 20 '11 at 18:47
  • @jerrymouse It happens to everyone. `id` is even used in the standard library as a name because it's so convenient. Ignore the downvoters, this was a perfectly fine question. – agf Sep 20 '11 at 18:48