Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Profiler throws NameError on any function

Reply
Thread Tools

Profiler throws NameError on any function

 
 
Philipp Lies
Guest
Posts: n/a
 
      03-26-2009
Hi,

I'm trying to run the python profiler on some code but I always get
NameErrors, even for the simplest case taken from the docs:
import profile
def foo():
a = 5
def prof():
profile.run('foo()')

When I run prof() I get the following output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "dummy.py", line 11, in prof
profile.run('foo()')
File "/usr/lib/python2.5/profile.py", line 70, in run
prof = prof.run(statement)
File "/usr/lib/python2.5/profile.py", line 456, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.5/profile.py", line 462, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'foo' is not defined
The very same error I get using cProfile.

It works when I call
profile.runctx('foo()', globals(), locals())
which should be the same as run('foo()'), shouldn't it?

I'm using python 2.5.2 on ubuntu 8.10.

Cheers

Phil
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-28-2009
En Thu, 26 Mar 2009 11:42:57 -0300, Philipp Lies
<(E-Mail Removed)> escribió:

> I'm trying to run the python profiler on some code but I always get
> NameErrors, even for the simplest case taken from the docs:
> import profile
> def foo():
> a = 5
> def prof():
> profile.run('foo()')
>
> When I run prof() I get the following output:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "dummy.py", line 11, in prof
> profile.run('foo()')
> File "/usr/lib/python2.5/profile.py", line 70, in run
> prof = prof.run(statement)
> File "/usr/lib/python2.5/profile.py", line 456, in run
> return self.runctx(cmd, dict, dict)
> File "/usr/lib/python2.5/profile.py", line 462, in runctx
> exec cmd in globals, locals
> File "<string>", line 1, in <module>
> NameError: name 'foo' is not defined
> The very same error I get using cProfile.
>
> It works when I call
> profile.runctx('foo()', globals(), locals())
> which should be the same as run('foo()'), shouldn't it?


Not exactly -- profile.run doesn't "extract" globals and locals from the
calling frame, as you appear to assume. It simply uses the namespace from
the __main__ module:

(profile.c, class Profile):
def run(self, cmd):
import __main__
dict = __main__.__dict__
return self.runctx(cmd, dict, dict)

This works when the called function is actually in the __main__ module.
From your traceback, you're first *importing* dummy.py and then *calling*
prof(). Call prof() directly inside dummy.py and it should work. That is,
add this line at the end:
prof()
and invoke it using: python dummpy.py

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy Profiler - Compile-time profiler for C++ potatosoftware C++ 0 11-04-2009 04:46 PM
Is there any good free(open source) Java profiler Colin Song Java 7 02-12-2009 07:17 AM
nameerror upon calling function seancron Python 1 09-01-2007 04:46 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Does anyone know if any cable station is running "Profiler"? need episode 4 season 1 Film Buff DVD Video 3 04-30-2004 03:13 PM



Advertisments