简体   繁体   English

就sys模块字典而言,使用-package vs module导入时会发生什么?

[英]In terms of sys module dictionary, what happens when importing by using - package vs module?

In terms of sys module dictionary, writing import modulename or from modulename import function/variable loads entire module into the sys modules dictionary table. 就sys模块词典而言,编写import modulename模块import modulenamefrom modulename import function/variable写入会将整个模块加载到sys模块词典表中。

Does import package also behave in same way. 导入包的行为是否也相同。 That is - when using import package or from package import modulename ; 那是-当使用import packagefrom package import modulename ; does this load all modules from the package into the sys modules dictionary table? 这会将包中的所有模块加载到sys模块词典表中吗?

sys.modules mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. sys.modules映射充当所有先前导入的模块(包括中间路径)的缓存。 So if foo.bar.baz was previously imported, sys.modules will contain entries for foo, foo.bar, and foo.bar.baz. 因此,如果以前导入了foo.bar.baz,则sys.modules将包含foo,foo.bar和foo.bar.baz的条目。 Each key will have as its value the corresponding module object. 每个键都有对应的模块对象作为其值。

When any python package is imported then its __init__.py file is implicitly executed and extra bindings are added into the sys.modules because of this. 导入任何python软件包时,都会隐式执行其__init__.py文件,并因此将额外的绑定添加到sys.modules中。

Like if you import python requests package then the following entries are added to the sys.modules 就像您导入python requests包一样,以下条目也会添加到sys.modules

requests
requests.exceptions
requests.__version__
requests.utils
requests.certs
requests._internal_utils
requests.compat
requests.cookies
requests.structures
requests.models
requests.hooks
requests.auth
requests.status_codes
requests.api
requests.sessions
requests.adapters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM