Profiler throws NameError on any function

Discussion in 'Python' started by Philipp Lies, Mar 26, 2009.

  1. Philipp Lies

    Philipp Lies Guest

    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
    Philipp Lies, Mar 26, 2009
    #1
    1. Advertising

  2. En Thu, 26 Mar 2009 11:42:57 -0300, Philipp Lies
    <> 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
    Gabriel Genellina, Mar 28, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    1
    Views:
    407
    Adam Maass
    Aug 14, 2003
  2. seancron

    nameerror upon calling function

    seancron, Sep 1, 2007, in forum: Python
    Replies:
    1
    Views:
    346
    Arnaud Delobelle
    Sep 1, 2007
  3. Colin Song
    Replies:
    7
    Views:
    4,539
    pinto
    Feb 12, 2009
  4. Weng Tianxiang
    Replies:
    7
    Views:
    1,257
    Paul Uiterlinden
    Sep 11, 2009
  5. potatosoftware
    Replies:
    0
    Views:
    655
    potatosoftware
    Nov 4, 2009
Loading...

Share This Page