Put this in the top-level module code:
import traceback last_frame = traceback.extract_stack()[-2] print 'Module imported from file:line_no = %s:%i' % last_frame[:2]
You can also use inspect instead of traceback :
import inspect last_frame = inspect.stack()[1] print 'Module imported from file:line_no = %s:%i' % last_frame[1:3]
source share