"ImportError" No module named 'bge' in OSX
Blender Python

Blender Python can be executed via the [run script] button. It will make Blender run your code immediately.

BGE Python

BGE Python code is executed by a Python controller. You need to ensure the controller gets triggered by at least one connected sensor. Be aware the sensor(s) do not need have a positive evaluation status to run your code. Your code needs to check the sensor status by itself.

This happens only when your game session is running (Blender is suspended during this time).

There are two ways to set up Python controllers:

Script mode

You can select one of the text blocks that are included in your .blend file. 

When triggered the python code inside the text block gets executed starting with the first line of the file. As usual any variable defined in the code will die latest after exiting your code block.

Example: myScript

import bge

controller = bge.logic.getCurrentController()

print( controller.name + "is currently executed" )


You configure your Python controller in Script mode. The field should be set to myScript.

Module mode

This means you created a python module. Python modules have a name ending with ".py". 

The BGE expects one function definition with either no or exactly one argument. When present the argument will receive the reference to the executing (current) controller.

The module can reside inside of the blend file as text block or outside of the blend somewhere in the Python search path. The path of the currently executed .blend file (the one your started your game from) is part of that path. 

Example: myModule.py

import bge

print("'myModule' is initializing")

def demonstrateExecution(controller):    
    print( controller.name + "is currently executed" )


You configure your Python controller in Module mode. The field should be set to myModule.demonstrateExecution (no parameter, no parenthesis).