python.sys.modules模块

sys.modules是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对于加载模块起到了缓冲的作用。当某个模块第一次导入,字典sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。

字典sys.modules具有字典所拥有的一切方法,可以通过这些方法了解当前的环境加载了哪些模块

import sys
print(sys.modules[__name__])
print(sys.modules.values())
print(sys.modules.keys())
print(sys.modules.items())

执行结果

>>> import sys
>>> print(sys.modules[__name__])
<module '__main__' (built-in)>
>>> print(sys.modules.values())
dict_values([<module 'builtins' (built-in)>, <module 'sys' (built-in)>, <module 'importlib._bootstrap' (frozen)>, <module '_imp' (built-in)>, <module '_warnings' (built-in)>, <module '_thread' (built-in)>, <module '_weakref' (built-in)>, <module 'importlib._bootstrap_external' (frozen)>, <module 'io' (built-in)>, <module 'marshal' (built-in)>, <module 'posix' (built-in)>, <module 'zipimport' (built-in)>, <module 'encodings' from '/home/nick/anaconda3/lib/python3.6/encodings/__init__.py'>, <module 'codecs' from '/home/nick/anaconda3/lib/python3.6/codecs.py'>, <module '_codecs' (built-in)>, <module 'encodings.aliases' from '/home/nick/anaconda3/lib/python3.6/encodings/aliases.py'>, <module 'encodings.utf_8' from '/home/nick/anaconda3/lib/python3.6/encodings/utf_8.py'>, <module '_signal' (built-in)>, <module '__main__' (built-in)>, <module 'encodings.latin_1' from '/home/nick/anaconda3/lib/python3.6/encodings/latin_1.py'>, <module 'io' from '/home/nick/anaconda3/lib/python3.6/io.py'>, <module 'abc' from '/home/nick/anaconda3/lib/python3.6/abc.py'>, <module '_weakrefset' from '/home/nick/anaconda3/lib/python3.6/_weakrefset.py'>, <module 'site' from '/home/nick/anaconda3/lib/python3.6/site.py'>, <module 'os' from '/home/nick/anaconda3/lib/python3.6/os.py'>, <module 'errno' (built-in)>, <module 'stat' from '/home/nick/anaconda3/lib/python3.6/stat.py'>, <module '_stat' (built-in)>, <module 'posixpath' from '/home/nick/anaconda3/lib/python3.6/posixpath.py'>, <module 'genericpath' from '/home/nick/anaconda3/lib/python3.6/genericpath.py'>, <module 'posixpath' from '/home/nick/anaconda3/lib/python3.6/posixpath.py'>, <module '_collections_abc' from '/home/nick/anaconda3/lib/python3.6/_collections_abc.py'>, <module '_sitebuiltins' from '/home/nick/anaconda3/lib/python3.6/_sitebuiltins.py'>, <module 'sysconfig' from '/home/nick/anaconda3/lib/python3.6/sysconfig.py'>, <module '_sysconfigdata_m_linux_x86_64-linux-gnu' from '/home/nick/anaconda3/lib/python3.6/_sysconfigdata_m_linux_x86_64-linux-gnu.py'>, <module '_bootlocale' from '/home/nick/anaconda3/lib/python3.6/_bootlocale.py'>, <module '_locale' (built-in)>, <module 'types' from '/home/nick/anaconda3/lib/python3.6/types.py'>, <module 'functools' from '/home/nick/anaconda3/lib/python3.6/functools.py'>, <module '_functools' (built-in)>, <module 'collections' from '/home/nick/anaconda3/lib/python3.6/collections/__init__.py'>, <module 'operator' from '/home/nick/anaconda3/lib/python3.6/operator.py'>, <module '_operator' (built-in)>, <module 'keyword' from '/home/nick/anaconda3/lib/python3.6/keyword.py'>, <module 'heapq' from '/home/nick/anaconda3/lib/python3.6/heapq.py'>, <module '_heapq' from '/home/nick/anaconda3/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so'>, <module 'itertools' (built-in)>, <module 'reprlib' from '/home/nick/anaconda3/lib/python3.6/reprlib.py'>, <module '_collections' (built-in)>, <module 'weakref' from '/home/nick/anaconda3/lib/python3.6/weakref.py'>, <module 'collections.abc' from '/home/nick/anaconda3/lib/python3.6/collections/abc.py'>, <module 'importlib' from '/home/nick/anaconda3/lib/python3.6/importlib/__init__.py'>, <module 'importlib._bootstrap' (frozen)>, <module 'importlib._bootstrap_external' (frozen)>, <module 'warnings' from '/home/nick/anaconda3/lib/python3.6/warnings.py'>, <module 'importlib.util' from '/home/nick/anaconda3/lib/python3.6/importlib/util.py'>, <module 'importlib.abc' from '/home/nick/anaconda3/lib/python3.6/importlib/abc.py'>, <module 'importlib.machinery' from '/home/nick/anaconda3/lib/python3.6/importlib/machinery.py'>, <module 'contextlib' from '/home/nick/anaconda3/lib/python3.6/contextlib.py'>, <module 'mpl_toolkits' (namespace)>, <module 'sphinxcontrib' from '/home/nick/anaconda3/lib/python3.6/site-packages/sphinxcontrib/__init__.py'>, <module 'readline' from '/home/nick/anaconda3/lib/python3.6/lib-dynload/readline.cpython-36m-x86_64-linux-gnu.so'>, <module 'atexit' (built-in)>, <module 'rlcompleter' from '/home/nick/anaconda3/lib/python3.6/rlcompleter.py'>])
>>> print(sys.modules.keys())
dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp', '_warnings', '_thread', '_weakref', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'zipimport', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_weakrefset', 'site', 'os', 'errno', 'stat', '_stat', 'posixpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', 'sysconfig', '_sysconfigdata_m_linux_x86_64-linux-gnu', '_bootlocale', '_locale', 'types', 'functools', '_functools', 'collections', 'operator', '_operator', 'keyword', 'heapq', '_heapq', 'itertools', 'reprlib', '_collections', 'weakref', 'collections.abc', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'warnings', 'importlib.util', 'importlib.abc', 'importlib.machinery', 'contextlib', 'mpl_toolkits', 'sphinxcontrib', 'readline', 'atexit', 'rlcompleter'])
>>> print(sys.modules.items())
dict_items([('builtins', <module 'builtins' (built-in)>), ('sys', <module 'sys' (built-in)>), ('_frozen_importlib', <module 'importlib._bootstrap' (frozen)>), ('_imp', <module '_imp' (built-in)>), ('_warnings', <module '_warnings' (built-in)>), ('_thread', <module '_thread' (built-in)>), ('_weakref', <module '_weakref' (built-in)>), ('_frozen_importlib_external', <module 'importlib._bootstrap_external' (frozen)>), ('_io', <module 'io' (built-in)>), ('marshal', <module 'marshal' (built-in)>), ('posix', <module 'posix' (built-in)>), ('zipimport', <module 'zipimport' (built-in)>), ('encodings', <module 'encodings' from '/home/nick/anaconda3/lib/python3.6/encodings/__init__.py'>), ('codecs', <module 'codecs' from '/home/nick/anaconda3/lib/python3.6/codecs.py'>), ('_codecs', <module '_codecs' (built-in)>), ('encodings.aliases', <module 'encodings.aliases' from '/home/nick/anaconda3/lib/python3.6/encodings/aliases.py'>), ('encodings.utf_8', <module 'encodings.utf_8' from '/home/nick/anaconda3/lib/python3.6/encodings/utf_8.py'>), ('_signal', <module '_signal' (built-in)>), ('__main__', <module '__main__' (built-in)>), ('encodings.latin_1', <module 'encodings.latin_1' from '/home/nick/anaconda3/lib/python3.6/encodings/latin_1.py'>), ('io', <module 'io' from '/home/nick/anaconda3/lib/python3.6/io.py'>), ('abc', <module 'abc' from '/home/nick/anaconda3/lib/python3.6/abc.py'>), ('_weakrefset', <module '_weakrefset' from '/home/nick/anaconda3/lib/python3.6/_weakrefset.py'>), ('site', <module 'site' from '/home/nick/anaconda3/lib/python3.6/site.py'>), ('os', <module 'os' from '/home/nick/anaconda3/lib/python3.6/os.py'>), ('errno', <module 'errno' (built-in)>), ('stat', <module 'stat' from '/home/nick/anaconda3/lib/python3.6/stat.py'>), ('_stat', <module '_stat' (built-in)>), ('posixpath', <module 'posixpath' from '/home/nick/anaconda3/lib/python3.6/posixpath.py'>), ('genericpath', <module 'genericpath' from '/home/nick/anaconda3/lib/python3.6/genericpath.py'>), ('os.path', <module 'posixpath' from '/home/nick/anaconda3/lib/python3.6/posixpath.py'>), ('_collections_abc', <module '_collections_abc' from '/home/nick/anaconda3/lib/python3.6/_collections_abc.py'>), ('_sitebuiltins', <module '_sitebuiltins' from '/home/nick/anaconda3/lib/python3.6/_sitebuiltins.py'>), ('sysconfig', <module 'sysconfig' from '/home/nick/anaconda3/lib/python3.6/sysconfig.py'>), ('_sysconfigdata_m_linux_x86_64-linux-gnu', <module '_sysconfigdata_m_linux_x86_64-linux-gnu' from '/home/nick/anaconda3/lib/python3.6/_sysconfigdata_m_linux_x86_64-linux-gnu.py'>), ('_bootlocale', <module '_bootlocale' from '/home/nick/anaconda3/lib/python3.6/_bootlocale.py'>), ('_locale', <module '_locale' (built-in)>), ('types', <module 'types' from '/home/nick/anaconda3/lib/python3.6/types.py'>), ('functools', <module 'functools' from '/home/nick/anaconda3/lib/python3.6/functools.py'>), ('_functools', <module '_functools' (built-in)>), ('collections', <module 'collections' from '/home/nick/anaconda3/lib/python3.6/collections/__init__.py'>), ('operator', <module 'operator' from '/home/nick/anaconda3/lib/python3.6/operator.py'>), ('_operator', <module '_operator' (built-in)>), ('keyword', <module 'keyword' from '/home/nick/anaconda3/lib/python3.6/keyword.py'>), ('heapq', <module 'heapq' from '/home/nick/anaconda3/lib/python3.6/heapq.py'>), ('_heapq', <module '_heapq' from '/home/nick/anaconda3/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so'>), ('itertools', <module 'itertools' (built-in)>), ('reprlib', <module 'reprlib' from '/home/nick/anaconda3/lib/python3.6/reprlib.py'>), ('_collections', <module '_collections' (built-in)>), ('weakref', <module 'weakref' from '/home/nick/anaconda3/lib/python3.6/weakref.py'>), ('collections.abc', <module 'collections.abc' from '/home/nick/anaconda3/lib/python3.6/collections/abc.py'>), ('importlib', <module 'importlib' from '/home/nick/anaconda3/lib/python3.6/importlib/__init__.py'>), ('importlib._bootstrap', <module 'importlib._bootstrap' (frozen)>), ('importlib._bootstrap_external', <module 'importlib._bootstrap_external' (frozen)>), ('warnings', <module 'warnings' from '/home/nick/anaconda3/lib/python3.6/warnings.py'>), ('importlib.util', <module 'importlib.util' from '/home/nick/anaconda3/lib/python3.6/importlib/util.py'>), ('importlib.abc', <module 'importlib.abc' from '/home/nick/anaconda3/lib/python3.6/importlib/abc.py'>), ('importlib.machinery', <module 'importlib.machinery' from '/home/nick/anaconda3/lib/python3.6/importlib/machinery.py'>), ('contextlib', <module 'contextlib' from '/home/nick/anaconda3/lib/python3.6/contextlib.py'>), ('mpl_toolkits', <module 'mpl_toolkits' (namespace)>), ('sphinxcontrib', <module 'sphinxcontrib' from '/home/nick/anaconda3/lib/python3.6/site-packages/sphinxcontrib/__init__.py'>), ('readline', <module 'readline' from '/home/nick/anaconda3/lib/python3.6/lib-dynload/readline.cpython-36m-x86_64-linux-gnu.so'>), ('atexit', <module 'atexit' (built-in)>), ('rlcompleter', <module 'rlcompleter' from '/home/nick/anaconda3/lib/python3.6/rlcompleter.py'>)])
>>>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,230评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,261评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,089评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,542评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,542评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,544评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,922评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,578评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,816评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,576评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,658评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,359评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,937评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,920评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,859评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,381评论 2 342

推荐阅读更多精彩内容