python sys.modules模块

Posted 随心朝阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sys.modules模块相关的知识,希望对你有一定的参考价值。

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[__name__])
print(sys.modules.values())
print(sys.modules.keys())
print(sys.modules.items())
<module __main__ from D:/python自动化21/untitled/07/上课/序列化.py>

dict_values([<module builtins (built-in)>, <module sys (built-in)>, <module _frozen_importlib (frozen)>, <module _imp (built-in)>, <module _warnings (built-in)>, <module _thread (built-in)>, <module _weakref (built-in)>, <module _frozen_importlib_external (frozen)>, <module io (built-in)>, <module marshal (built-in)>, <module nt (built-in)>, <module winreg (built-in)>, <module zipimport (built-in)>, <module encodings from D:\\python36\\lib\\encodings\\__init__.py>, <module codecs from D:\\python36\\lib\\codecs.py>, <module _codecs (built-in)>, <module encodings.aliases from D:\\python36\\lib\\encodings\\aliases.py>, <module encodings.utf_8 from D:\\python36\\lib\\encodings\\utf_8.py>, <module _signal (built-in)>, <module __main__ from D:/python自动化21/untitled/07/上课/序列化.py>, <module encodings.latin_1 from D:\\python36\\lib\\encodings\\latin_1.py>, <module io from D:\\python36\\lib\\io.py>, <module abc from D:\\python36\\lib\\abc.py>, <module _weakrefset from D:\\python36\\lib\\_weakrefset.py>, <module site from D:\\python36\\lib\\site.py>, <module os from D:\\python36\\lib\\os.py>, <module errno (built-in)>, <module stat from D:\\python36\\lib\\stat.py>, <module _stat (built-in)>, <module ntpath from D:\\python36\\lib\\ntpath.py>, <module genericpath from D:\\python36\\lib\\genericpath.py>, <module ntpath from D:\\python36\\lib\\ntpath.py>, <module _collections_abc from D:\\python36\\lib\\_collections_abc.py>, <module _sitebuiltins from D:\\python36\\lib\\_sitebuiltins.py>, <module sysconfig from D:\\python36\\lib\\sysconfig.py>]) dict_keys([builtins, sys, _frozen_importlib, _imp, _warnings, _thread, _weakref, _frozen_importlib_external, _io, marshal, nt, winreg, zipimport, encodings, codecs, _codecs, encodings.aliases, encodings.utf_8, _signal, __main__, encodings.latin_1, io, abc, _weakrefset, site, os, errno, stat, _stat, ntpath, genericpath, os.path, _collections_abc, _sitebuiltins, sysconfig]) dict_items([(builtins, <module builtins (built-in)>), (sys, <module sys (built-in)>), (_frozen_importlib, <module _frozen_importlib (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 _frozen_importlib_external (frozen)>), (_io, <module io (built-in)>), (marshal, <module marshal (built-in)>), (nt, <module nt (built-in)>), (winreg, <module winreg (built-in)>), (zipimport, <module zipimport (built-in)>), (encodings, <module encodings from D:\\python36\\lib\\encodings\\__init__.py>), (codecs, <module codecs from D:\\python36\\lib\\codecs.py>), (_codecs, <module _codecs (built-in)>), (encodings.aliases, <module encodings.aliases from D:\\python36\\lib\\encodings\\aliases.py>), (encodings.utf_8, <module encodings.utf_8 from D:\\python36\\lib\\encodings\\utf_8.py>), (_signal, <module _signal (built-in)>), (__main__, <module __main__ from D:/python自动化21/untitled/07/上课/序列化.py>), (encodings.latin_1, <module encodings.latin_1 from D:\\python36\\lib\\encodings\\latin_1.py>), (io, <module io from D:\\python36\\lib\\io.py>), (abc, <module abc from D:\\python36\\lib\\abc.py>), (_weakrefset, <module _weakrefset from D:\\python36\\lib\\_weakrefset.py>), (site, <module site from D:\\python36\\lib\\site.py>), (os, <module os from D:\\python36\\lib\\os.py>), (errno, <module errno (built-in)>), (stat, <module stat from D:\\python36\\lib\\stat.py>), (_stat, <module _stat (built-in)>), (ntpath, <module ntpath from D:\\python36\\lib\\ntpath.py>), (genericpath, <module genericpath from D:\\python36\\lib\\genericpath.py>), (os.path, <module ntpath from D:\\python36\\lib\\ntpath.py>), (_collections_abc, <module _collections_abc from D:\\python36\\lib\\_collections_abc.py>), (_sitebuiltins, <module _sitebuiltins from D:\\python36\\lib\\_sitebuiltins.py>), (sysconfig, <module sysconfig from D:\\python36\\lib\\sysconfig.py>)])

 



以上是关于python sys.modules模块的主要内容,如果未能解决你的问题,请参考以下文章

Python 模块未出现在 sys.modules 中

python 的sys.modules的用法

python 模块知识

python sys.modules来获得当前加载的模块,使用进口可以添加要加载的模块。可以用名字来过滤模块成员。名称仅仅是名字不带命名空间

常用的模块 一 --sys --os ---haslib摘要算法

os和sys模块