简体   繁体   中英

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.

Does import package also behave in same way. That is - when using import package or from package import modulename ; does this load all modules from the package into the sys modules dictionary table?

sys.modules mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So if foo.bar.baz was previously imported, sys.modules will contain entries for foo, foo.bar, and 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.

Like if you import python requests package then the following entries are added to the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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