I am attempting to do some scripting using blender 2.79 for OSX. When using Blender Game, I attempt to create a python script with the BGE. The first and only line in my test script is:

import bge

When I run this script I get the message in the terminal window:

ImportError: No module named 'bge' Error: Python script fail, look in the console for now...

Blender Game is loaded in the user preferences. I do not get this problem in Blender 2.79 for the Windows platform, but I will be using this primarily on OSX. I also do not get this when I employ the following:

import bpy

Any thoughts?

  • $\begingroup$ How are you running this script? The ImportError is expected in the script window & some when the bge isn't running. $\endgroup$
    – dr. Sybren
    Oct 18 '17 at 20:41
  • $\begingroup$ I'm using the blender Text Editor. I create a new script, type in "import bge" and hit the "run script" button. I know it is recognized as code, since the word "import" is changed to magenta and "syntax highlighting" is turned on. This is the same thing I've done successfully on the windows platform. $\endgroup$ Oct 18 '17 at 20:56
  • 1
    $\begingroup$ I test it in v2.78 on windows 10 and i think that you can't run a bge script hiting "run script" because you are in the scope of blender (bpy), instead you need to configurate the logics to run the script and press P to lunch bge, as you mention it does not shown an error on v2.79, i assume that if you put more lines the script will do nothing or will throw an error, have you tried it? $\endgroup$ Oct 19 '17 at 0:06

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).

  • $\begingroup$ Inserting the "what script is running" text above into my script, and then running the game produced the proper result. I incorrectly assumed that I could test code outside of running the game session. Apparently BGE requires the game session to be running. Thank you! That helped a lot. I'll be adding a lot of little "code testers" in the app I'm working on and then running it to debug. $\endgroup$ Oct 19 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.