def test_import_pytorch_lightning_with_torch_dist_unavailable():
    """Test that the package can be imported regardless of whether torch.distributed is available."""
    code = dedent(
        """
        import torch
        try:
           # PyTorch 2.5 relies on torch,distributed._composable.fsdp not
           # existing with USE_DISTRIBUTED=0
           import torch._dynamo.variables.functions
           torch._dynamo.variables.functions._fsdp_param_group = None
        except ImportError:
           pass

        # pretend torch.distributed not available
        for name in list(torch.distributed.__dict__.keys()):
            if not name.startswith("__"):
                delattr(torch.distributed, name)

        torch.distributed.is_available = lambda: False

        # needed for Dynamo in PT 2.5+ compare the torch.distributed source
        class _ProcessGroupStub:
            pass
        torch.distributed.ProcessGroup = _ProcessGroupStub

        import lightning.pytorch
        """
    )
    # run in complete isolation
    assert subprocess.call([sys.executable, "-c", code]) == 0
Test that the package can be imported regardless of whether torch.distributed is available.
tests/tests_pytorch/utilities/test_imports.py