sys.modules是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对于加载模块起到了缓冲的作用。当某个模块第一次导入,字典sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。
字典sys.modules具有字典所拥有的一切方法,可以通过这些方法了解当前的环境加载了哪些模块。
import sys
print sys.modules.keys()
print sys.modules.values()
print sys.modules["os"]
>>> import sys
>>> print sys.modules.keys()
['apt.os', 'heapq', 'email.iterators', '_io', 'functools', 'random', 'apport.errno', '_bisect', 'email.mime.text', 'subprocess', 'sysconfig', 'email.MIMEImage', 'gc', 'apport.pwd', 'apt.re', 'encodings.encodings', 'email.mime', 'email.MIMEText', 'apport.sys', 'xml', 'email.time', 'struct', 'tempfile', 'apport.traceback', 'base64', 'apt.cache', 'pyexpat.errors', 'email.uu', 'apt.operator', 'email.warnings', 'collections', 'apt_pkg', 'apport', 'email.binascii', 'email.Parser', 'zipimport', 'string', 'xml.dom.copy', 'apt.urllib2', 'apport.hashlib', 'encodings.utf_8', 'apt.apt_pkg', 'ssl', 'apport.re', 'apt', 'email.urllib', 'email.FeedParser', 'ConfigParser', 'httplib', 'bisect', 'signal', 'email.codecs', 'apt.progress.errno', 'email.encoders', 'pyexpat.model', 'apport.packaging_impl', 'quopri', 'email.Message', 'cStringIO', 'zlib', 'rfc822', 'locale', 'apt.progress.sys', 'email.charset', 'apport.fileutils', 'xml.parsers.expat', 'email.quopriMIME', 'apt.weakref', 'apt.progress.warnings', 'encodings', 'email.Generator', 'apport.warnings', 'apport.problem_report', 'abc', 'email.MIMEAudio', 'apport.ConfigParser', 'apt.httplib', 'urllib', 're', 'apt.deprecation', 'apt.progress.re', 'email.quopri', 'apport.report', 'email.mime.base', 'apt.subprocess', 'email.errors', 'email', 'math', 'fcntl', 'apport.os', 'apt.progress', 'apt.progress.os', 'UserDict', 'urllib2', 'fnmatch', 'apport.grp', 'apport.shutil', 'codecs', 'apport.urllib', 'email.Header', '_functools', '_locale', 'email.Iterators', 'socket', 'thread', 'traceback', 'apt.apt', 'apt.progress.text', 'apt.fnmatch', 'weakref', 'itertools', 'apt.progress.apt', 'email.Charset', 'apt.progress.fcntl', 'xml.dom.xmlbuilder', 'os', 'marshal', 'apport.stat', '__future__', '_collections', 'apt.progress.select', 'xml.dom', '_sre', '__builtin__', 'apport.apport', 'xml.parsers', 'apport.fnmatch', 'xml.dom.domreg', 'operator', 'xml.parsers.pyexpat', 'array', 'email.Errors', 'select', '_heapq', 'apport.glob', 'apt.warnings', 'email.socket', 'posixpath', 'email.base64MIME', 'errno', '_socket', 'binascii', 'email.Utils', 'sre_constants', 'email.MIMEMessage', 'email._parseaddr', 'email.sys', 'os.path', 'apt.package', 'apt.socket', '_warnings', 'xml.dom.NodeFilter', 'apport.gettext', 'encodings.__builtin__', 'email.MIMENonMultipart', '_codecs', 'apport.apt', 'email.os', 'email.utils', 'pwd', 'apport.time', 'copy', '_struct', 'email.email', 'hashlib', 'apt.cdrom', 'keyword', 'uu', 'xml.dom.minidom', 'apport_python_hook', 'email.random', 'posix', 'encodings.aliases', 'apt.sys', 'exceptions', 'sre_parse', 'pickle', 'mimetools', 'copy_reg', 'sre_compile', '_hashlib', '_random', 'site', 'email.base64', 'io', '__main__', 'problem_report', 'apt.progress.old', 'pyexpat', 'email.MIMEBase', 'email.message', 'apport.xml', 'email.mime.nonmultipart', 'apport.subprocess', 'shutil', 'strop', 'grp', 'linecache', 'encodings.codecs', 'gettext', '_abcoll', 'xml.dom.minicompat', 'email.MIMEMultipart', 'apport.tempfile', 'genericpath', 'stat', '_ssl', 'warnings', 'glob', 'email.re', 'textwrap', 'sys', 'email.Encoders', 'readline', 'apt.glob', 'email.cStringIO', 'types', 'xml.dom.xml', 'apt.progress.base', 'sitecustomize', 'email.mime.email', 'apt.progress.apt_pkg', 'email.base64mime', 'email.mime.multipart', '_weakref', 'email.quoprimime', 'apport.packaging', 'urlparse', '_weakrefset', 'email.string', 'time', 'gzip', 'apt.collections']
>>> print sys.modules.values()
[None,
>>> print sys.modules["os"]