Simon Willison’s Weblog

Subscribe

Why does Python load imported modules separately for different files, unlike C or PHP? Isn’t that inefficient in terms of memory usage?

27th October 2010

My answer to Why does Python load imported modules separately for different files, unlike C or PHP? Isn’t that inefficient in terms of memory usage? on Quora

It doesn’t—you’re misunderstanding how Python’s module system works. If two different places have “import os” in them, the os module is only imported and executed once—it’s cached in the sys.modules dictionary so you can see it happen if you want to. The key thing to understand is that “import os” attaches the os module to the “os” symbol within the current file’s scope, loading it only if it hasn’t been loaded already.

This is Why does Python load imported modules separately for different files, unlike C or PHP? Isn’t that inefficient in terms of memory usage? by Simon Willison, posted on 27th October 2010.

Next: What are all the advantages of jQuery?

Previous: What is the best lightweight jQuery tooltip plugin? Why?