'Import Random' Error

I’m trying use a Gateway Event Script to create a 16-digit randomly generated string every 10 seconds.

My script looks like:

import string
import random

def string_generator(size=16):
	return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(size))

system.tag.write('folder/tag',string_generator())

This works fine when testing in the Script Console. However, when I try to run it as a Gateway Event Script it does not.

The wrapper log shows the error:

INFO   | jvm 1    | 2018/03/09 16:02:49 | E [c.i.i.c.s.TimerScriptTask     ] [21:02:49]: Error executing global timer script: HSES/randomString @10,000ms . Repeat errors of this type will be logged as 'debug' messages. 
INFO   | jvm 1    | 2018/03/09 16:02:49 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 8, in <module>
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 5, in string_generator
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 5, in <genexpr>
INFO   | jvm 1    | 2018/03/09 16:02:49 | NameError: global name 'random' is not defined
INFO   | jvm 1    | 2018/03/09 16:02:49 | 
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.Py.NameError(Py.java:260)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyFrame.getglobal(PyFrame.java:265)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.f$2(<TimerScript:HSES/randomString @10,000ms >:5)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyGenerator.__iternext__(PyGenerator.java:149)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyGenerator.__iternext__(PyGenerator.java:131)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PySequence.fastSequence(PySequence.java:241)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyString.str_join(PyString.java:1876)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyString$str_join_exposer.__call__(Unknown Source)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyObject.__call__(PyObject.java:391)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.string_generator$1(<TimerScript:HSES/randomString @10,000ms >:5)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyBaseCode.call(PyBaseCode.java:114)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyFunction.__call__(PyFunction.java:307)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.f$0(<TimerScript:HSES/randomString @10,000ms >:11)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.pycode._pyx864.call_function(<TimerScript:HSES/randomString @10,000ms >)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.PyCode.call(PyCode.java:18)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at org.python.core.Py.runCode(Py.java:1275)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:634)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:601)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at java.util.TimerThread.mainLoop(Unknown Source)
INFO   | jvm 1    | 2018/03/09 16:02:49 | 	at java.util.TimerThread.run(Unknown Source)
INFO   | jvm 1    | 2018/03/09 16:02:49 | Caused by: org.python.core.PyException: Traceback (most recent call last):
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 8, in <module>
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 5, in string_generator
INFO   | jvm 1    | 2018/03/09 16:02:49 |   File "<TimerScript:HSES/randomString @10,000ms >", line 5, in <genexpr>
INFO   | jvm 1    | 2018/03/09 16:02:49 | NameError: global name 'random' is not defined

Any information explaining why import random would not be working would be appreciated.

The scope changes inside the function. You’ll need to import it there, instead.

2 Likes

That worked. Thank you sir.