0

From earlier questions on SO like this and this, it seems that the simple import keyword does not do lazy loading of modules. However, when I run a simple test to check the memory usage before and after the import statement - it seems that some modules are indeed loaded lazily.

Example:

>>> import django

>>> from django.http import HttpResponse

Memory Usage (the first free command is run after the first import and the second free command is run after the second import above):

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3935        1773         399           0        1762        1889

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3935        1787         386           0        1762        1875

Note the increase in used memory from 1773MB to 1787MB.

I am trying to understand when does a module actually get loaded in memory? And why does an import django command not load all of django modules in memory?

diviquery
  • 569
  • 5
  • 19
  • @MisterMiyagi I was expecting that `import django` would indeed load all of the modules within django. But as pointed out in the answer by @Ron Serruya, it seems like the django init file doesn't load those modules, so it makes sense now. – diviquery Jun 10 '21 at 08:28

2 Answers2

4

it seems that the simple import keyword does not do lazy loading of modules.

How should it? import has to guarantee the code in the imported file / module's __init__.py is executed at the point of importing.

Just try the following: make a file test.py, containing nothing but print("hey!"), and in the same directory, import .test. You'll notice that import really isn't different from just executing the code in test.py.

I am trying to understand when does a module actually get loaded in memory?

import name really just executes the specified code in the current namespace under the name namespace. If you define a class in that code, then its instantly visible, has to get parsed, and whatnot.

And why does an import django command not load all of django modules in memory?

Because the code that gets executed on import django simply doesn't do that. (just as it doesn't get up and make us the much-deserved coffee!)

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
1

What import some_pgk do is to run the some_pkg.__init__ file

This is the init file for django

from django.utils.version import get_version

VERSION = (4, 0, 0, 'alpha', 0)

__version__ = get_version(VERSION)


def setup(set_prefix=True):
    ...
    ...

As you can see it doesn't do much, it only sets the version, this is why other modules (such as django.http) don't get importer when running import django

Ron Serruya
  • 3,988
  • 1
  • 16
  • 26