How to make a module reload in python after the script is compiled?

Problem summary:

A test_it.py program is running and has a predicate available, e.g. is_odd(). Every few minutes, a newly written file containing a revised is_odd() predicate becomes available, and test_it wishes to feed a test vector to revised predicate.

There are several straightforward solutions.

  1. Don't load the predicate in the current process at all. Serialize the test vector, send it to a newly forked child which computes and serializes results, and examine those results.
  2. Typically eval is evil, but here you might want that, or exec.
  3. Replace current process with a newly initialized interpreter: https://docs.python.org/3/library/os.html#os.execl
  4. Go the memory leak route. Use a counter to assign each new file a unique module name, manipulate source file to match, and load that. As a bonus, this makes it easy to diff current results against previous results.
  5. Reload: from importlib import reload