Return value for importlib.import_module()

I have a strage problem with importlib.import_module(). I cannot find documentation which return value is expected. It seems that return values sometimes different.

I'm using python3.8 with Django 2.2 and there is a function import_string() https://github.com/django/django/blob/stable/2.2.x/django/utils/module_loading.py#L17

def import_string(dotted_path):
    """
    Import a dotted module path and return the attribute/class designated by the
    last name in the path. Raise ImportError if the import failed.
    """
    try:
        module_path, class_name = dotted_path.rsplit('.', 1)
    except ValueError as err:
        raise ImportError("%s doesn't look like a module path" % dotted_path) from err

    module = import_module(module_path)

    try:
        return getattr(module, class_name)
    except AttributeError as err:
        raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
            module_path, class_name)
        ) from err

The line module = import_module(module_path) imports a module, and sometimes this module has attributes, but sometimes it doesn't.

So for example if I do print(dir(module)) sometime I see object like this:

['BaseCache', 'CONNECTION_INTERRUPTED', 'ConnectionInterrupted', 'DJANGO_REDIS_SCAN_ITERSIZE', 'RedisCache', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'functools', 'import_string', 'logging', 'omit_exception', 'settings']

But sometimes it looks like this:

['__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

So obviously for second case next line after import fails: return getattr(module, class_name)

I wonder why this may happen? I think it's not backend library problem because I saw similar errors for redis backend and mysql backend. Also the problem is that it happen only on ecs server and I cannot reproduce it locally.

Also I put imports for modules which fails to init.py in my app and it helps. Also helps to put some code to hit redis and mysql backends on start. But anyway I'd like to figure out what's wrong because not sure where else it can fail. Thanks in advance.