Long Xayah
Long Xayah

Reputation: 1

Do identical imports in different modules "stack" in memory?

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

Answers (1)

Brad Figueroa
Brad Figueroa

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

Related Questions