import importlibsys if len(sys argv modname sys argv[ module importlib import_module(modnamereloader(moduleself-test code import on tests only command line (or passedimport by name string test passed-in reloader if __name__ ='__main__'tester(reload_all'reloadall'testreload myselfbesides namespace dictionariesthis script makes use of other tools we've studied hereit includes __name__ test to launch self-test code when run as top-level script onlyand its tester function uses sys argv to inspect command-line arguments and impor tlib to import module by name string passed in as function or command-line argument one curious bitnotice how this code must wrap the basic reload call in try statement to catch exceptions--in python reloads sometimes fail due to rewrite of the import machinery the try was previewed in and is covered in full in part vii testing recursive reloads nowto leverage this utility for normal useimport its reload_all function and pass it an already loaded module object--just as you would for the built-in reload function when the file runs standaloneits self-test code calls reload_all automaticallyreloading its own module by default if no command-line arguments are used in this modethe module must import itself because its own name is not defined in the file without an import this code works in both and because we've used and instead of comma in the printsthough the set of modules used and thus reloaded may vary across linesc:\codec:\python \python reloadall py reloading reloadall reloading types :\codec:\python \python reloadall py reloading reloadall reloading types with command-line argumentthe tester instead reloads the given module by its name string--herethe benchmark module we coded in note that we give module name in this modenot filename (as for import statementsdon' include the py extension)the script ultimately imports the module using the module search path as usualc:\codereloadall py pybench reloading pybench reloading timeit reloading itertools reloading sys reloading time reloading gc reloading os exampletransitive module reloads