The trouble you are experiencing is due to the fact that runScript() is prefixing the string you feed it with an assignment statement to a hidden variable. So the code that actually executes in Paul’s first suggestion looks like this:
__someVar = import random; random.randint(0, 1000)
And then the
__someVar value is fished out of the local variable table to feed to the binding. Not valid python.
If you add a real newline to your string expression, you can
print dir() to see what that variable name is. You can also assign a constant temporarily, with a newline, import, execute anything, and assign a real return value several lines later.