sys.modules 以字典形式储存程序的所有模块 , __name__当前模块
例如:
print(__name__) print(sys.modeles) print(hasattr(sys.modules[__name__],'A')) # 可用于映射当前模块是否有‘A’对象或类输出:
__main__
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (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)>, 'encodings': <module 'encodings' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\encodings\\__init__.py'>, 'codecs': <module 'codecs' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\encodings\\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\encodings\\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'F:/python/路飞学城/python学习记录(天)/练习.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\encodings\\latin_1.py'>, 'io': <module 'io' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\io.py'>, 'abc': <module 'abc' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site.py'>, 'os': <module 'os' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\os.py'>, 'stat': <module 'stat' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\stat.py'>, '_stat': <module '_stat' (built-in)>, '_collections_abc': <module '_collections_abc' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\_collections_abc.py'>, 'ntpath': <module 'ntpath' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\ntpath.py'>, 'genericpath': <module 'genericpath' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\genericpath.py'>, 'os.path': <module 'ntpath' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\ntpath.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\_sitebuiltins.py'>, 'traceback': <module 'traceback' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\traceback.py'>, 'collections': <module 'collections' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\collections\\__init__.py'>, 'operator': <module 'operator' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\operator.py'>, '_operator': <module '_operator' (built-in)>, 'keyword': <module 'keyword' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\keyword.py'>, 'heapq': <module 'heapq' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\heapq.py'>, '_heapq': <module '_heapq' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'reprlib': <module 'reprlib' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\reprlib.py'>, '_collections': <module '_collections' (built-in)>, 'linecache': <module 'linecache' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\linecache.py'>, 'functools': <module 'functools' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\functools.py'>, '_functools': <module '_functools' (built-in)>, 'tokenize': <module 'tokenize' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\tokenize.py'>, 're': <module 're' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\re.py'>, 'enum': <module 'enum' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\enum.py'>, 'types': <module 'types' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\types.py'>, 'sre_compile': <module 'sre_compile' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\sre_compile.py'>, '_sre': <module '_sre' (built-in)>, 'sre_parse': <module 'sre_parse' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\sre_parse.py'>, 'sre_constants': <module 'sre_constants' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\sre_constants.py'>, '_locale': <module '_locale' (built-in)>, 'copyreg': <module 'copyreg' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\copyreg.py'>, 'token': <module 'token' from 'C:\\Users\\huawang\\AppData\\Local\\Programs\\Python\\Python37\\lib\\token.py'>, 'sitecustomize': <module 'sitecustomize' from 'E:\\谷歌下载\\pycharm\\PyCharm 2020.2\\plugins\\python\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>}True