import os,traceback,sys globalvar = 'hello, world!' from submod import * # This doesn't work: print '#### Test 1', '#'*40 try: greet() except: traceback.print_exc(file=sys.stdout) # Neither do these: print; print '#### Test 2', '#'*40 try: exec greet.func_code except: traceback.print_exc(file=sys.stdout) print; print '#### Test 3', '#'*40 try: exec greet.func_code in locals() except: sys.stdout.flush() traceback.print_exc(file=sys.stdout) sys.stderr.flush() # But this does: print; print '#### Test 4', '#'*40 try: exec file(os.path.splitext(sys.modules[greet.__module__].__file__)[0] + '.py', 'r') except: traceback.print_exc(file=sys.stdout) # This doesn't: it re-invokes *this* script (recursively...) print; print '#### Test 5', '#'*40 try: exec file(os.path.splitext(sys.modules[greet.__module__].__file__)[0] + '.py', 'r') in locals() except: traceback.print_exc(file=sys.stdout) sys.exit(1) print 'end-of-test'