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.
More recent articles
- A new SQL-powered permissions system in Datasette 1.0a20 - 4th November 2025
- New prompt injection papers: Agents Rule of Two and The Attacker Moves Second - 2nd November 2025
- Hacking the WiFi-enabled color screen GitHub Universe conference badge - 28th October 2025