Profiling from *within* a module
Leo Breebaart
leo at lspace.org
Tue Oct 7 09:52:15 EDT 2003
Hi all,
I'm new to Python, and I have hit upon something that I just cannot seem
to solve or understand using the available documentation.
Given the file 'leotest.py':
---------------------------------------------------------------------------
""" leotest.py - test ways of profiling python functions. """
def foo():
pass
def test():
for i in range(10000): foo()
def myprofile():
import profile
profile.run('test()')
if __name__ == "__main__":
myprofile()
---------------------------------------------------------------------------
If I call this as "python leotest.py", everything works and a profile
is generated:
[cygnus] % python leotest.py
10003 function calls in 0.040 CPU seconds
[...]
Profiling from the Python interpreter is also no problem:
>>> import profile
>>> import leotest
>>> profile.run('leotest.test()')
10003 function calls in 0.030 CPU seconds
[...]
But try to have the profile called from 'within' the module, and Python
just doesn't seem to want to know:
>>> leotest.myprofile()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "leotest.py", line 11, in myprofile
profile.run('test()')
File "/usr/lib/python2.3/profile.py", line 71, in run
prof = prof.run(statement)
File "/usr/lib/python2.3/profile.py", line 403, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.3/profile.py", line 409, in runctx
exec cmd in globals, locals
File "<string>", line 1, in ?
NameError: name 'test' is not defined
I think I'm simply misunderstanding how modules and functions and scopes
interact in Python, but I can't discover where exactly I'm going wrong
-- and I can't find a workaround either. Surely it is *somehow* possible
to do what I want, i.e. initiate the profile from within the module
itself? After all, it works in the command-line version!
Would anybody here be willing to explain this behaviour to me?
Many thanks in advance,
--
Leo Breebaart <leo at lspace.org>
More information about the Python-list
mailing list