Python interactive mode not registering changes made to file on disk

pythonterminal

I am running Python 2.7 in interactive mode in Terminal. If I make changes to the Python source or data file while the same is open in Python prompt, it doesn't appear to see the change, and exhibits the same behavior as if no change had been made.

When I quit() Python and start it up again, then it registers the change.

Any ideas how do I achieve the desired? I also have Python 3.7 installed on my Mac. I am running macOS Sierra.

Best Answer

Use the execfile(...) built-in function, to reload the Python source file without quitting the interactive session.

execfile(...) execfile(filename[, globals[, locals]])

Read and execute a Python script from a file. The globals and locals are dictionaries, defaulting to the current globals and locals. If only globals is given, locals defaults to it.