Reload a fixture module after changing it
importlib.reload() re-executes a module that was imported successfully, using its original loader. This example creates a temporary directory containing fixture_reload.py , adds that directory to sys.path , and imports the fixture. Its first concrete value is "one" . The source is then replaced with a longer assignment whose value is "two changed" ; invalidate_caches() asks import finders to discard relevant discovery caches before reload() is called. Assertions check the first value, that reload returned the same module object in this case, and the newly defined value. The output contains only the final value, so it does not depend on the temporary directory name. The finally block removes this fixture's path entry and module-cache entry even if an assertion fails. Changing the file length also makes this small fixture less dependent on a filesystem timestamp with coarse resolution. Reloading is not a general reset mechanism. Python retains the module dic...