Lightning-AI/pytorch-lightning · info

Redirecting import of {module}.{name} to {new_module}.{name}

Error message

Redirecting import of {module}.{name} to {new_module}.{name}

What it means

This warning comes from Lightning's migration unpickler (used when loading old checkpoints/hparams pickled with legacy 'pytorch_lightning' module paths). If the environment only has the 'lightning' package (mirror package), old pickle streams referencing pytorch_lightning.* classes are redirected to lightning.pytorch.* equivalents, and each redirected import is warned about. It is informational: the object loads fine, just under the new module path.

Source

Thrown at src/lightning/pytorch/utilities/migration/utils.py:197

    target_version = Version(target)
    is_lte_max_version = max_version is None or target_version <= Version(max_version)
    return is_lte_max_version and Version(_get_version(checkpoint)) < target_version


class _RedirectingUnpickler(pickle._Unpickler):
    """Redirects the unpickling of `pytorch_lightning` classes to `lightning.pytorch`.

    In legacy versions of Lightning, callback classes got pickled into the checkpoint. These classes are defined in the
    `pytorch_lightning` but need to be loaded from `lightning.pytorch`.

    """

    @override
    def find_class(self, module: str, name: str) -> Any:
        new_module = _patch_pl_to_mirror_if_necessary(module)
        # this warning won't trigger for standalone as these imports are identical
        if module != new_module:
            warnings.warn(f"Redirecting import of {module}.{name} to {new_module}.{name}")
        return super().find_class(new_module, name)


def _patch_pl_to_mirror_if_necessary(module: str) -> str:
    _pl = "pytorch_" + "lightning"  # avoids replacement during mirror package generation
    if module.startswith(_pl):
        # for the standalone package this won't do anything,
        # for the unified mirror package it will redirect the imports
        return "lightning.pytorch" + module[len(_pl) :]
    return module

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Treat it as informational — the unpickling succeeds; no code change is strictly required.
  2. Install the standalone 'pytorch_lightning' shim alongside 'lightning' (pip install pytorch-lightning) so old module paths resolve identically and no redirect is needed.
  3. Re-save/migrate artifacts with the new 'lightning.pytorch.*' paths (e.g. re-serialize model and hparams under the new package) to eliminate future warnings.
  4. Suppress with warnings.filterwarnings('ignore', message='Redirecting import of.*') if log noise is a problem.

Example fix

# before
model = MyModule.load_from_checkpoint("old_pl1_checkpoint.ckpt")  # warns on each legacy import

# after
import warnings
warnings.filterwarnings("ignore", message="Redirecting import of.*")
model = MyModule.load_from_checkpoint("old_pl1_checkpoint.ckpt")
# then re-save under lightning>=2.0 to stop future redirects
new_ckpt = {k: v for k, v in torch.load("old_pl1_checkpoint.ckpt", map_location="cpu").items()}
torch.save(new_ckpt, "migrated.ckpt")
Defensive patterns

Strategy: fallback

Validate before calling

import pickletools  # optional inspection
def artifact_uses_legacy_paths(path) -> bool:
    with open(path, "rb") as f:
        head = f.read(65536)
    return b"pytorch_lightning" in head

Try / catch

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", message="Redirecting import of.*")
    obj = torch.load("legacy.ckpt", map_location="cpu")  # redirect fallback still applies internally

Prevention

When it happens

Trigger: Unpickling a checkpoint, hparams.yaml, or saved object that references classes under the old 'pytorch_lightning' namespace (e.g. pytorch_lightning.core.module.LightningModule) while running in an environment where only the unified 'lightning' package is installed, so _patch_pl_to_mirror_if_necessary rewrites the module string before super().find_class resolves it. Triggered by torch.load(..., pickle_module)/Lightning load, or pl migration utilities scanning old checkpoints.

Common situations: Resuming training from checkpoints created with pytorch-lightning<2.0 after upgrading to lightning>=2.0; loading old hyperparameter pickles; environments where pytorch_lightning shim is absent or the mirror package strips the legacy path; CI logs filled with these warnings after a dependency upgrade.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/6bf37a247666c77b. Report an issue: GitHub.