Search code examples
pythonimportread-eval-print-loop

Why does importing module again not updates the module?


I was going through a tutorial on exception handling and had to write this piece of code in module called exception.py

def convert(s):
    try:
        x = int(s)
        prnit("Conversion done. x=", x)  #there is a typo
    except ValueError:
        print("Failed")
        x = -1
    return x

then from the REPL i import this function as

from exception import convert

and do convert(7.7) which returned the NameError: name 'prnit' is not defined as expected. After which I corrected typo and did from exception import convert again. But the error remained. Why didn't it import new module?

I had to exit() then REPL and import it again then it worked fine and as expected.


Solution

  • Imports are cached in Python, you can read about it in the official documentation.

    There are ways to invalidate the import cache, but it's strongly discouraged.