Search code examples
pythonpython-3.xpython-importpython-module

Get an ordered list of imported modules in python


Is there a way to get a list of imported modules that are listed as imported?

globals() returns a dict which is unordered.


Solution

  • Although it's not guaranteed that the dictionary is ordered in CPython-3.6 it is ordered in the current 3.6 versions of CPython.

    So if you display sys.modules (a dictionary containing all loaded modules) it should be ordered by the "relative order of imports":

    import sys
    
    print(list(sys.modules))