How setup Cycles Compute Device in console
In Blender 2.79 you can use:

import bpy

preferences = bpy.context.user_preferences
cycles_preferences = preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = cycles_preferences.get_devices()

cycles_preferences.compute_device_type = "CUDA"

for device in cuda_devices:
    print('Activating {}'.format(device.name))
    device.use = True


In Blender 2.80 you can use:

import bpy

preferences = bpy.context.preferences
cycles_preferences = preferences.addons['cycles'].preferences
cuda_devices, opencl_devices = cycles_preferences.get_devices()

cycles_preferences.compute_device_type = "CUDA"

for device in cuda_devices:
    print(f'Activating {device.name}')
    device.use = True


Be aware that the script doesn't perform error handling for the case that the device isn't CUDA capable. You will get a TypeError with an OpenCL device.