I'm a new python/Blender user I'm trying to move an object using an Arduino. I've watched many videos and tutorials, and somehow i could write a code using others codes. here is my code

import bge
import math
import serial

controller = bge.logic.getCurrentController()
own = controller.owner

serialport = serial.Serial('COM4', 9600)
sens = controller.sensors['run']

p = pi/255

def run():
    if sens.positive: 
            serialport.write('s'.encode('ascii')) #blender sends the ascii code of "s" to the Arduino
            n = serialport.readline() #receive data from the arduino
            val1 = (float) (n)
            val2 = (val1-val3)* p
            val3 = val1
            print ('The value is: ')
            print (val2)
            own.applyRotation((0 , val2 , 0),0)

#    own.position.y = val2

my problem is with the sens = controller.sensors['run'] when i run the game engine the following message appears:

Python module can't be imported - object 'Cube', controller 'Python': KeyError: 'requested item "run" does not exist'

as usual i looked for a solution by checking questions of people have the same problem here and elsewhere. so i followed some of the best answers. such as, setting the actor flag in physics tab, some people suggested that reinstalling blender would help. nothing worked for me. thanks anyway

  • $\begingroup$ Maybe you are trying to access a sensor from other controller or the sensor simply does not exist, i can't be sure since you are not showing images from the logic editor, so take a look to bse answer : sensors and controllers $\endgroup$ Commented Oct 10, 2017 at 1:28
  • $\begingroup$ Here is the correct link i post the wrong link lol. $\endgroup$ Commented Oct 10, 2017 at 5:53

2 Answers 2


The line controller = bge.logic.getCurrentController() gets the controller logic brick that has the python script, then controller.sensors is a list of sensors that are connected to the python controller. The 'key' that you use with the sensor list must match one of the sensor names connected to the controller.

example logic bricks

So (1) is the connection required for the sensor to be in the controller.sensors list. Then (2) and (3) must be an exact match which is case sensitive.

So your error indicates that the name of run does not match any sensors you have connected to the python controller.

Having said that, you don't need to get the sensor for the script you have. I expect you are using an always sensor to run the script repeatedly, you should only need to read from your arduino and perform any changes you want.

The main flaw you will find is that the script is run cleanly from the start each time, that means the variables you are adjusting will be reset to zero every time. You want to use your script as a module rather than a script, this answer should explain the difference.

  • $\begingroup$ this worked for me thanks bro apparently i needed to name the sensor and the controller $\endgroup$
    – Bassel
    Commented Oct 10, 2017 at 18:26


Python module can't be imported - object 'Cube', controller 'Python': KeyError: 'requested item "run" does not exist'

This means you misconfigured the Python controller.

Either you use script mode and you refer to an not existing text block called "run" (the field should have red background).

Or you use module mode and you did not mention what file contains your code.

E.g. your code is called arduino.py. You can set up this line: arduino.applyRotation.

The BGE will look for arduino.py and check if it finds a function applyRotation.


import bge 

def applyRotation(controller):
    sensor = controller.sensors["run"]
    if sensor.positive:


Ensure the file and the function inside of it can be found.


If you want to use the module mode ensure to retrieve the controller inside of the called function. Otherwise you get unwanted side effects.

  • $\begingroup$ I already added an Always sensor and match it to a Python controller called "Python". and i changed it's mode to module and i refer it to "run" as follows test11.run however it keeps showing the error. can you please explain what do u mean by retrieving the controller inside of the called function? Thanks anyway $\endgroup$
    – Bassel
    Commented Oct 10, 2017 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .