python - cProfile not running
Asked Answered
B

2

6

I was trying to run a performance test of my code using cProfile, but sadly no matter how I tried cProfile refused to function properly. Here's what I did:

import cProfile
cProfile.run('addNum()')  # addNum() is a very simple function that adds a bunch of 
                          # numbers into a dictionary

and here's what I got:

Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Python27\Lib\cProfile.py", line 36, in run
result = prof.print_stats(sort)
File "C:\Python27\Lib\cProfile.py", line 81, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "C:\Python27\Lib\pstats.py", line 81, in __init__
self.init(arg)
File "C:\Python27\Lib\pstats.py", line 95, in init
self.load_stats(arg)
File "C:\Python27\Lib\pstats.py", line 124, in load_stats
self.__class__, arg)
TypeError: Cannot create or construct a <class pstats.Stats at 0x01AE9CA8> object from '<cProfile.Profile object at 0x01ACC470>''

Can someone help me debug this, and hopefully provide a solution?

I'm running Python 2.7.3 on Wing IDE 101 ver4.1.

Thank you!!!

Blockhead answered 3/7, 2012 at 3:59 Comment(0)
P
2

This seems like a problem with the pStats module and not the cProfile.

can you try doing

import pstats

If this says cannot import pstats, then try installing the python-profiler again. It comes with python itself but that might be messed up in your case IF pstats is not there.

It's a simple apt-get on linux, so I am assuming windows would have a separate binary for python-profiler too.

Hope this helps!

Pazice answered 3/7, 2012 at 6:35 Comment(2)
I have tried importing pstats, but it still reported the same error. =[Blockhead
No, I meant just try writing import pstats in a shell. Just check if its there or not, and if it is there, then do you get some other errors or not. If it imports successfully, then you would need to see if the module is not bad or something. Try out some of the functions mentioned here docs.python.org/library/profile.html for pstats in a separate file ( not in your main code, just to check if pstats is working fine.)Pazice
S
0

I've had the same problem with Python 3.5.2 today:

What ended up working was replacing the call which I want to profile like this, then running the whole program:

import cProfile
# myObject.myFunc()
cProfile.runctx('myObject.myFunc()', globals(), locals(), 'myFunc.stat')

Finally, in an interactive python3 running separately, I did:

>>> import pstats
>>> p = pstats.Stats('myFunc.stat')
>>> p.strip_dirs().sort_stats(-1).print_stats()
Wed Feb 20 17:10:05 2019    myFunc.stat

         10218759 function calls (3916491 primitive calls) in 16.519 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   16.519   16.519 <string>:1(<module>)
   ... the really useful stats followed here ...

The cProfile.runctx(...), globals(), locals() were necessary to fix a NameError I ran into; the TypeError which you asked about was fixed by specifying a file name to store the stats into, which is also available via the normal cProfile.run(...)

Synergism answered 21/2, 2019 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.