sys.modules 与sys.modules[__name__] 的理解

sys.modules是一个字典,存储了程序中所有已导入的模块信息,包括内置和用户自定义模块。而__name__变量表示当前模块的名字,通常在运行脚本时,它的值为'__main__'。此文章通过示例展示了如何查看和理解这两个概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值