3

I make two files:

#test_func.py
def test():
    print('hello')

and

#test_inspect.py
import inspect
import test_func

reload(inspect)
reload(test_func)
reload(inspect)
reload(test_func)

print inspect.getsource(test_func.test)

Running import test_inspect from IPython or other interactive shell prints the right thing:

def test():
    print('hello')

but if I edit and save test_func.py to be:

#test_func.py
def test():
    print('good bye')

I still get:

def test():
    print('hello')

when I run reload(test_inspect) from the shell. How can I convince the inspect module to re-read the source file?

(If you must know why I want to do this, I will elaborate in the comments, but for right now, I'd like to simply know if there is a workaround for this or if there is something fundamental about the inspect module that prevents this)

Paul
  • 42,322
  • 15
  • 106
  • 123

1 Answers1

5

This should do the trick:

import linecache
linecache.clearcache()
omz
  • 53,243
  • 5
  • 129
  • 141
  • Ahh. Much better. Thank you. – Paul May 07 '13 at 00:51
  • 1
    I think this is actually a bug in `linecache` or `inspect` which should be fixed in python 3.5. Thanks for the workaround. See https://bugs.python.org/issue1218234 – EL_DON Jan 16 '19 at 18:08