Martin Lipovský
Martin Lipovský

Reputation: 89

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.

Upvotes: 5

Views: 4281

Answers (1)

MSeifert
MSeifert

Reputation: 152775

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))

Upvotes: 4

Related Questions