How can I get the line number ?

Maxim Khitrov mkhitrov at
Fri Jul 24 20:59:04 CEST 2009

On Fri, Jul 24, 2009 at 2:51 PM, kk<maymunbeyin at> wrote:
> Hello
> I am writing some Python code that runs in another application(has
> wrapper functions). Due to lack of debugging I am printing out alot of
> outputs and manual messages. I want to be able to create a function
> that would let me print the current line number that is called from.
> This is not for debugging exceptions it is rather to simplify my debug
> messages, at least I can trace my debug messages.
> thanks

Modify the following as needed:

from inspect import currentframe, getframeinfo

def caller_info(depth=0):
	Get file, line number, and name of the calling function.
	if depth < 0:
		raise ValueError('invalid stack depth')

	caller = frame = currentframe()
		for i in xrange(-1, depth):
			caller = caller.f_back
			if caller is None:
				return (None, None, None)

		return getframeinfo(caller, 0)[:3]
		del caller, frame

- Max

More information about the Python-list mailing list