Zoom to selection in Python gives RuntimeError
Operators need a certain context to run from, so you can override this so it can be run from the text editor or wherever depending on the area you need it executed from:

import bpy

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        for region in area.regions:
            if region.type == 'WINDOW':
                override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object}
                bpy.ops.view3d.view_all(override)


Read more in the docs and see this related question for more info.