Check if MPS is available before importing torch

Hey everyone,
I have a hen and egg problem with import torch. I need to set the environmental variable os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = str(1), before import torch otherwise it will have no effect. However, I only need to set it if torch.backends.mps.is_available() is TRUE. But to check this, I would have needed to have importetd torch before for what I would have needed to know, if MPS would be available. You see my problem? Any easy solution for that?

You could set the env variable globally since it won’t have any effect of MPS is not available.