Is there a way to explicitly set the mode in python?
You can go directly to a specific mode using -

bpy.ops.object.mode_set(mode='EDIT', toggle=False)


Valid mode values are 'OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT', 'PARTICLE_EDIT', 'POSE' but not all will be accepted at anytime, eg PARTICLE_EDIT will only be accepted if the object has a particle system.