Lightning-AI/pytorch-lightning · error · FileNotFoundError

Path {str(filename)!r} does not exist or is not a file.

Error message

Path {str(filename)!r} does not exist or is not a file.

What it means

`_lazy_load` opens a checkpoint with `torch.PyTorchFileReader`, which requires an existing regular file. Before touching the file it checks `os.path.isfile` and raises FileNotFoundError with the offending path if it is missing, a directory, or a non-file (e.g. an in-memory object or URL).

Source

Thrown at src/lightning/fabric/utilities/load.py:213

            return partial(_NotYetLoadedTensor.rebuild_parameter, archiveinfo=self)
        return super().find_class(module, name)

    @override
    def persistent_load(self, pid: tuple) -> "TypedStorage":
        from torch.storage import TypedStorage

        _, cls, _, _, _ = pid
        with warnings.catch_warnings():
            # The TypedStorage APIs have heavy deprecations in torch, suppress all these warnings for now
            warnings.simplefilter("ignore")
            storage = TypedStorage(dtype=cls().dtype, device="meta")
        storage.archiveinfo = pid
        return storage


def _lazy_load(filename: _PATH) -> Any:
    if not os.path.isfile(filename):
        raise FileNotFoundError(f"Path {str(filename)!r} does not exist or is not a file.")
    file_reader = torch.PyTorchFileReader(str(filename))
    with BytesIO(file_reader.get_record("data.pkl")) as pkl:
        mup = _LazyLoadingUnpickler(pkl, file_reader)
        return mup.load()


def _materialize_tensors(collection: Any) -> Any:
    def _load_tensor(t: _NotYetLoadedTensor) -> Tensor:
        return t._load_tensor()

    return apply_to_collection(collection, dtype=_NotYetLoadedTensor, function=_load_tensor)


def _move_state_into(
    source: dict[str, Any], destination: dict[str, Union[Any, _Stateful]], keys: Optional[set[str]] = None
) -> None:
    """Takes the state from the source destination and moves it into the destination dictionary.

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Verify the path exists and is a file: `os.path.isfile(path)`; print the absolute path you're actually passing
  2. Use absolute paths or anchor paths to the script/dir (e.g. `Path(__file__).parent`)
  3. Download remote checkpoints to local disk before calling load_checkpoint

Example fix

// before
state = _lazy_load("checkpoints/model")  # directory or missing

// after
from pathlib import Path
ckpt = Path("checkpoints/model.ckpt").resolve()
assert ckpt.is_file(), f"missing {ckpt}"
state = _lazy_load(str(ckpt))
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.path.isfile(str(path)):
    raise FileNotFoundError(f"checkpoint not found: {os.path.abspath(path)}")

Try / catch

try:
    state = _lazy_load(path)
except FileNotFoundError:
    logger.error("checkpoint missing: %s", path)
    raise

Prevention

When it happens

Trigger: Calling `load_checkpoint`/`_lazy_load` with a wrong path, a directory, an `os.PathLike` that doesn't exist, a relative path resolved from the wrong working directory, or an fsspec/URL-style path.

Common situations: Typos in checkpoint paths, running the script from a different CWD so relative paths break, passing a directory instead of the checkpoint file, or paths on remote storage that need local download first.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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