Way to detect if a valid GPU device is available from Python Blender allows the user to select &quot;GPU Compute&quot; as the rendering device even if the computer has no valid GPU device. It simply Greys out the drop down menu:

To a Python script, it then looks like a GPU compute device is in use:

I need a way to check from my Python script if an actual valid GPU device will be used for rendering or not.
Cycles will silently default to the CPU if the user selects GPU but doesn't have one, but I don't know how to detect this from a script.
A bit of ferreting around yielded this:
import bpy
import _cycles

cycles_preferences = bpy.context.preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = cycles_preferences.get_devices()
for device in cuda_devices:
    if device.use == True:
        print(&quot;Device -&quot;, device.name, &quot;is&quot;, device.use)

Basically, it will iterate through the available devices in the &quot;cuda_devices&quot; (you can iterate &quot;opencl_devices&quot; too) and &quot;device.use&quot; will be true if it is active. So if anything returns True you know a valid rendering device is active.
You can adapt it to suit your application.