{"record":{"id":"9f245ad9526086cd","repo":"Lightning-AI/pytorch-lightning","slug":"unable-to-determine-the-source-of-the-trainer","errorCode":null,"errorMessage":"Unable to determine the source of the trainer.","messagePattern":"Unable to determine the source of the trainer\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"info","filePath":"src/lightning/pytorch/trainer/connectors/callback_connector.py","lineNumber":102,"sourceCode":"        self.trainer.callbacks.extend(_load_external_callbacks(\"lightning.pytorch.callbacks_factory\"))\n        _validate_callbacks_list(self.trainer.callbacks)\n\n        # push all model checkpoint callbacks to the end\n        # it is important that these are the last callbacks to run\n        self.trainer.callbacks = self._reorder_callbacks(self.trainer.callbacks)\n\n    def _configure_checkpoint_callbacks(self, enable_checkpointing: bool) -> None:\n        if self.trainer.checkpoint_callbacks:\n            if not enable_checkpointing:\n                raise MisconfigurationException(\n                    \"Trainer was configured with `enable_checkpointing=False`\"\n                    \" but found `ModelCheckpoint` in callbacks list.\"\n                )\n        elif enable_checkpointing:\n            if RequirementCache(\"litmodels >=0.1.7\") and self.trainer._model_registry:\n                trainer_source = inspect.getmodule(self.trainer)\n                if trainer_source is None or not isinstance(trainer_source.__package__, str):\n                    raise RuntimeError(\"Unable to determine the source of the trainer.\")\n                # this need to imported based on the actual package lightning/pytorch_lightning\n                if \"pytorch_lightning\" in trainer_source.__package__:\n                    from litmodels.integrations.checkpoints import PytorchLightningModelCheckpoint as LitModelCheckpoint\n                else:\n                    from litmodels.integrations.checkpoints import LightningModelCheckpoint as LitModelCheckpoint\n\n                model_checkpoint = LitModelCheckpoint(model_registry=self.trainer._model_registry)\n            else:\n                # Defer the litmodels tip until loggers are set up (in _attach_model_callbacks)\n                self._pending_litmodels_tip = True\n                model_checkpoint = ModelCheckpoint()\n            self.trainer.callbacks.append(model_checkpoint)\n\n    def _configure_model_summary_callback(self, enable_model_summary: bool) -> None:\n        if not enable_model_summary:\n            return\n\n        model_summary_cbs = [type(cb) for cb in self.trainer.callbacks if isinstance(cb, ModelSummary)]","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/callback_connector.py#L84-L120","documentation":"Raised when the litmodels registry integration (litmodels >=0.1.7 installed and trainer._model_registry set) tries to determine which package the Trainer class came from via inspect.getmodule, but the module or its __package__ cannot be resolved. The integration must know whether it is lightning or pytorch_lightning to pick the correct LightningModelCheckpoint vs PytorchLightningModelCheckpoint class.","triggerScenarios":"Trainer(_model_registry=...) (or a subclass setting it) with litmodels installed, where the Trainer instance is created from a dynamically executed/reloaded module, a REPL, or an environment where inspect cannot map the class to an importable module with a string __package__.","commonSituations":"Running training from a notebook, jupyter cell with %run, exec'd scripts, frozen/compiled binaries (PyInstaller), or unusual import machinery (custom loaders) that break inspect module resolution.","solutions":["Pass the checkpoint callback explicitly, bypassing the auto-detection: from litmodels.integrations.checkpoints import LightningModelCheckpoint and add it to callbacks","Avoid setting _model_registry when running from dynamic-execution contexts","Run the training script as a normal imported module instead of exec/REPL","If packaging with PyInstaller, ensure hidden imports are declared so inspect can resolve modules"],"exampleFix":"# before\ntrainer = Trainer(max_epochs=2)  # _model_registry set by subclass, module not resolvable\n# after\nfrom litmodels.integrations.checkpoints import LightningModelCheckpoint\ntrainer = Trainer(max_epochs=2, callbacks=[LightningCheckpoint := LightningModelCheckpoint()])","handlingStrategy":"fallback","validationCode":"import inspect\nmod = inspect.getmodule(Trainer)\nassert mod is not None and isinstance(mod.__package__, str), \"Trainer module not resolvable; set callbacks manually\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid _model_registry in dynamic-exec contexts (REPL/exec/notebook %run)","Pass the litmodels checkpoint callback explicitly to skip introspection","Test your entrypoint as an imported module, matching production"],"tags":["lightning","litmodels","inspect","dynamic-import","checkpointing"],"backgroundTag":"module-introspection-failed","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}