Why does running a game engine script from the text editor cause an error?
The 'Run Script' button is not intended for BGE, but for Blender addons and macros. Understand that BGE is a standalone component, it's not part of Blender environment.

This way, importing the module bge is not possible when running scripts inside Blender, the same way you can't import the module bpy while running scripts through BGE.