profile.run won't run!

Rainer Deyke root at rainerdeyke.com
Fri Jun 15 00:22:22 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in message
news:UreW6.237306$eK2.50518869 at news4.rdc1.on.home.com...
> I can't get profile.run to work..
>
> def solve_problem():
>     ...blahblahblah
>
> solve_problem()          # works
> exec("solve_problem()")  # also works
>
> but..
>
> profile.run("solve_problem()")
>
> ...
>   File "c:\python21\lib\profile.py", line 356, in run
>     return self.runctx(cmd, dict, dict)
>   File "c:\python21\lib\profile.py", line 362, in runctx
>     exec cmd in globals, locals
>   File "<string>", line 1, in ?
> NameError: name 'solve_problem' is not defined
>
> Am I missing something?

'profile' operates on the module '__main__', which is the module of the
interactive interpreter and/or the main script.  If you are trying to call
it from a different module, try

  profile.run("import module;module.solve_problem()")

where 'module' is the name of the module that contains the function
'solve_problem'.  Alternately, try this:

  import __main__
  __main__.solve_problem = solve_problem
  profile.run('solve_problem')

This should work even if 'solve_problem' is not in a real module at all.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list