Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top