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`