{"record":{"id":"6bf37a247666c77b","repo":"Lightning-AI/pytorch-lightning","slug":"redirecting-import-of-module-name-to-new-modu","errorCode":null,"errorMessage":"Redirecting import of {module}.{name} to {new_module}.{name}","messagePattern":"Redirecting import of (.+?)\\.(.+?) to (.+?)\\.(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/lightning/pytorch/utilities/migration/utils.py","lineNumber":197,"sourceCode":"    target_version = Version(target)\n    is_lte_max_version = max_version is None or target_version <= Version(max_version)\n    return is_lte_max_version and Version(_get_version(checkpoint)) < target_version\n\n\nclass _RedirectingUnpickler(pickle._Unpickler):\n    \"\"\"Redirects the unpickling of `pytorch_lightning` classes to `lightning.pytorch`.\n\n    In legacy versions of Lightning, callback classes got pickled into the checkpoint. These classes are defined in the\n    `pytorch_lightning` but need to be loaded from `lightning.pytorch`.\n\n    \"\"\"\n\n    @override\n    def find_class(self, module: str, name: str) -> Any:\n        new_module = _patch_pl_to_mirror_if_necessary(module)\n        # this warning won't trigger for standalone as these imports are identical\n        if module != new_module:\n            warnings.warn(f\"Redirecting import of {module}.{name} to {new_module}.{name}\")\n        return super().find_class(new_module, name)\n\n\ndef _patch_pl_to_mirror_if_necessary(module: str) -> str:\n    _pl = \"pytorch_\" + \"lightning\"  # avoids replacement during mirror package generation\n    if module.startswith(_pl):\n        # for the standalone package this won't do anything,\n        # for the unified mirror package it will redirect the imports\n        return \"lightning.pytorch\" + module[len(_pl) :]\n    return module\n","sourceCodeStart":179,"sourceCodeEnd":208,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/migration/utils.py#L179-L208","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Treat it as informational — the unpickling succeeds; no code change is strictly required.","Install the standalone 'pytorch_lightning' shim alongside 'lightning' (pip install pytorch-lightning) so old module paths resolve identically and no redirect is needed.","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.","Suppress with warnings.filterwarnings('ignore', message='Redirecting import of.*') if log noise is a problem."],"exampleFix":"# before\nmodel = MyModule.load_from_checkpoint(\"old_pl1_checkpoint.ckpt\")  # warns on each legacy import\n\n# after\nimport warnings\nwarnings.filterwarnings(\"ignore\", message=\"Redirecting import of.*\")\nmodel = MyModule.load_from_checkpoint(\"old_pl1_checkpoint.ckpt\")\n# then re-save under lightning>=2.0 to stop future redirects\nnew_ckpt = {k: v for k, v in torch.load(\"old_pl1_checkpoint.ckpt\", map_location=\"cpu\").items()}\ntorch.save(new_ckpt, \"migrated.ckpt\")","handlingStrategy":"fallback","validationCode":"import pickletools  # optional inspection\ndef artifact_uses_legacy_paths(path) -> bool:\n    with open(path, \"rb\") as f:\n        head = f.read(65536)\n    return b\"pytorch_lightning\" in head","typeGuard":null,"tryCatchPattern":"import warnings\n\nwith warnings.catch_warnings():\n    warnings.filterwarnings(\"ignore\", message=\"Redirecting import of.*\")\n    obj = torch.load(\"legacy.ckpt\", map_location=\"cpu\")  # redirect fallback still applies internally","preventionTips":["After upgrading to lightning>=2.0, re-save checkpoints and hparams so they pickle under lightning.pytorch paths.","Pin consistent Lightning versions between training and inference environments to avoid path rewrites.","Filter this specific message in CI logs rather than blanket-ignoring all warnings."],"tags":["pytorch-lightning","migration","pickle","checkpoint","version-upgrade"],"backgroundTag":"legacy-module-import-redirect","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}