[SOLVED]Code Runs in Console but Fails in Text Editor

In the process of finding a solution to the previous posted issue, I’m looking into comparing the positions of an object at adjacent frames. Here is my code:

import bpy


ops = bpy.ops
scene = bpy.context.scene
ob = scene.objects[2]
scene.objects.active = ob


bpy.context.scene.frame_set(20)
pos1=Vector(ob.location)
bpy.context.scene.frame_set(21)
pos2=Vector(ob.location)
print('Pos\'s: ', pos2, pos1)
print('Delta: ', (pos2-pos1))




bpy.context.scene.frame_set(60)
pos1=Vector(ob.location)
bpy.context.scene.frame_set(61)
pos2=Vector(ob.location)
print('Pos\'s2: ', pos2, pos1)
print('Delta2: ', (pos2-pos1))

When I run it sequentially in the console, it works fine. But when I use “Run Script” in the text editor, it fails, without reaching any of the print statements. (Note, the “Vector(ob.location)” initializations are there because without them, all the pos values ended up using the position at frame 61, for some reason). What’s going wrong?

EDIT:

Found the solution: I needed to import mathutils at the start, then call mathutils.Vector