Since you're using something from bpy.ops
, you need to get the editor state right (this is the failing of most of bpy.ops
).
You probably need to set bpy.context.scene.objects.active = target
and in 2.8+
context.view_layer.objects.active = target