Compute Device Selection for a shared Blender Version
Okay. Robert's answer was the key.
Apparently the array in bpy.context.preferences.addons['cycles'].preferences.devices holds all devices that have ever been activated and saved in the blender gui. In order to get the current hardware and to update the devices array, you call:
preferences = bpy.context.preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = preferences.get_devices()

And then you do something like:
gpu_device = None

for device in preferences.devices:
    device.use = False #reset all other devices
    if device.type == &quot;CUDA&quot; and device in cuda_devices:
        gpu_device = device

bpy.context.scene.cycles.device = &quot;GPU&quot;
bpy.context.preferences.addons[&quot;cycles&quot;].preferences.compute_device_type = gpu_device.type
gpu_device.use = True

for scene in bpy.data.scenes:
    scene.render.engine = &quot;CYCLES&quot;
    scene.cycles.device = &quot;GPU&quot;