What's wrong with my batch script?
Due to some silly mistakes. This one works.
import bpy

objs = bpy.context.selected_objects

for obj in objs:
    bpy.ops.object.select_all(action='DESELECT')
    obj.select_set(True)
    bpy.context.view_layer.objects.active = obj

    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.uv.pack_islands(margin=0.001)
    bpy.ops.object.mode_set(mode='OBJECT')