Message 383271 - Python tracker
> Modules are already cleared in the reverse order. In Python 3.9, _PyImport_Cleanup() of Python/import.c contains the comment: /* Since dict is ordered in CPython 3.6+, modules are saved in importing order. First clear modules imported later. */ But using Python 3.9, the z.py example output is: --- a b c --- The most recently imported module is the last one to be deleted. The first loop iterates on sys.modules, not on reversed(sys.modules).