Reputation: 1
Suppose I have 2 files first.py
, second.py
and both files import the same module, lets say requests
. If first.py
imports second.py
would requests
be stacked twice in memory, or would python use some kind of caching system to import requests
once and use it in both files.
Upvotes: 0
Views: 49
Reputation: 875
Well, when you import
a module, functions and so on, the first thing Python
will do is look up the name request
in sys.modules, so this works as a cache of all modules that have been previously imported. If the name is not found, then Python
will check if module is part of the built-in modules and finally it will search for it in a list of directories defined by sys.path. Hope this can help you :)
Upvotes: 1