from pprint import pprint print(f"{'=' * 20}") print(f"Running main.py module name {__name__}") # __main__ is the starting point of wherever module we start # The code for module1 does not happen untill this import. Execs the code and puts it into the sys.modules cache import module1 # The pprint_dict will be part of the module1 globals which prints when we import module1 print(module1) module1.pprint_dict('main.globals', globals()) import sys pprint(sys.path) # Print the system module cache pprint(sys.modules) # Importing modules multiple times does nothing as they are already in the sys.modules cache # None of the module1 code will be run import module1 import module1 del globals()['module1'] # module1 no longer in the namespace, the module is still in memory and is referenced # by the sys.modules cache # it's in cache but not in the global namespace import module1 # The reference is brought back into the globals but the code isn't run again module1.pprint_dict('main.globals', globals()) print(f"{'=' * 20}")
print("===============================") print("Running main.py - module name: {0}".format(__name__)) import module1 print(module1) module1.pprint_dict("main.globals", globals()) print("IMPORTING module1 again.." ) #it wont import module1 again, it would first look into sys.modules import module1 print("===============================")
import sys print('=================================================\n\n') print('---- Running Main.py.. Module name {0} ----'.format(__name__)) import module1 print('Main.globals: ', module1.pprint_dict('main.globals()', globals())) # Python compiler looks for the imports in this sys.path print('System_paths:\n', sys.path) print('Where is module1:', sys.modules['module1']) print('\n\n=================================================') print('\n\n********************** Delete a Module *************************') del globals()['module1']
import sys # print(sys) # print(sys.prefix) # print(sys.path) print("=========================================") print(f"Running main.py - module name {__name__}") import module1 print(module1) # module1.pprint_dict("main.globals", globals()) print(sys.path) print(sys.modules['module1']) del globals()["module1"] import module1 # после удаления модуля, нужно заново его импортировать print(globals()) module1.pprint_dict("globals", globals()) # после удаления функция не работает print("=========================================") # print(sys.modules)