Error when loading model

Every time when I load a saved model which has Python script behaviour, I receive the following error:

Reading model from file: C:\Users\xxx\xxxx\Visual Components\4.1\My Models\MySimulation.vcmx
  Traceback (most recent call last):
    File "GenericRobot::PythonController", line 12, in <module>
    File "C:\Program Files\Visual Components\Visual Components Premium 4.1\Python\Commands\vcHelpers\Robot2.py", line 979, in getRobot
      return vcRobot2(roboComp, controller, executor, graspContainer, signalMapIn, signalMapOut)
    File "C:\Program Files\Visual Components\Visual Components Premium 4.1\Python\Commands\vcHelpers\Robot2.py", line 73, in __init__
      self.Program = executor.Program
    File "C:\Program Files\Visual Components\Visual Components Premium 4.1\Python\Commands\vcHelpers\Robot2.py", line 31, in __setattr__
      if name == 'Speed':
  ReferenceError: Executor does not have a program associated.

It is possible to fix this error by making some minor changes to the code (like adding a simple print statement) and compiling it.

This problem does not happen with models downloaded from VC academy. This problem happens only when I create a model from scratch and add a Python script behaviour.

Any suggestions?

You are probably using the getRobot method before the robot controller behaviour has been fully initialized during layout loading.

Yes, indeed. This is a sample code:
from vcScript import *
from vcHelpers.Robot2 import *

app = getApplication()
robot = getRobot()
conveyor = app.findComponent('Conveyor')
path = conveyor.findBehaviour('Path__HIDE__')
product_bin = app.findComponent('ProductBin')

def OnRun():
    pass

I managed to solve the problem by placing everything inside the OnRun() method:

from vcScript import *
from vcHelpers.Robot2 import *

def OnRun():
    app = getApplication()
    robot = getRobot()
    conveyor = app.findComponent('Conveyor')
    path = conveyor.findBehaviour('Path__HIDE__')
    product_bin = app.findComponent('ProductBin')
    pass