美文网首页
python.sys.modules模块

python.sys.modules模块

作者: Jarkata | 来源:发表于2021-09-27 16:19 被阅读0次

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

    相关文章

      网友评论

          本文标题:python.sys.modules模块

          本文链接:https://www.haomeiwen.com/subject/khxknltx.html