valueChange: global name 'shared' is not defined

I am getting a bizarre error from a script running on a tag ValueChange event.

The script calls a shared function, but I am getting an error that the global name ‘shared’ is not defined. I can open the script console and call this function without any problems. I also set up a test tag that called the same function and it worked fine.

The gateway is running 7.9.10. Anybody know what is causing this.

def getWeather() :
	if currentValue.value == 1 :
		system.tag.write("[.]Lift Start Time", system.date.now())
		shared.weather.getWeather()
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 5, in valueChanged NameError: global name 'shared' is not defined
at org.python.core.Py.NameError(Py.java:260)
at org.python.core.PyFrame.getglobal(PyFrame.java:265)
at org.python.pycode._pyx3.valueChanged$1(:32)
at org.python.pycode._pyx3.call_function()
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
at org.python.core.PyFunction.function___call__(PyFunction.java:376)
at org.python.core.PyFunction.__call__(PyFunction.java:371)
at org.python.core.PyFunction.__call__(PyFunction.java:361)
at org.python.core.PyFunction.__call__(PyFunction.java:356)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)
at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$FunctionInvokerImpl.run(TagScriptManager.java:493)
at com.inductiveautomation.ignition.common.sqltags.scripts.AbstractTagScript.invoke(AbstractTagScript.java:33)
at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$Task.invoke(TagScriptManager.java:442)
at com.inductiveautomation.ignition.common.sqltags.scripts.TagScriptManager$TagScriptDispatcher.run(TagScriptManager.java:405)
at com.inductiveautomation.ignition.common.execution.impl.BasicExecutionEngine$ThrowableCatchingRunnable.run(BasicExecutionEngine.java:518)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 5, in valueChanged NameError: global name 'shared' is not defined

... 23 common frames omitted

It’s because you’re defining a function inside of the valueChanged or whichever function. There’s a few areas where we have to use an older style of Python scoping for backwards compatibility reasons. Try adding import shared just below the def getWeather() line.

Sorry…

That was a typo…You can’t copy the first few lines in the tagEvent script and I mistyped it

The code inside the tagChange event is

def valueChanged(tagPath, previousValue, currentValue, initialChange, missedEvents):
	# when the light turns red, record the lift start time
	if currentValue.value == 1 :
		system.tag.write("[.]Lift Start Time", system.date.now())
		#shared.weather.getWeather()
		system.tag.write("[~]Weather/test tag", 1)

We are running 7.9.10 and are seeing the same thing on different windows, at random. Usually logging out and back into the client fixes it but it comes back again on/off. I don’t always have it on both windows within the same session either. I’ll get it on the window with the code below, but the other window I also get it on, is fine with the global name shared. You would think if it is something related to that session it would affect both windows but it is not.

Here is an snippet of the section code (pointed out in there error message) from one window, it is on a custom method. I included import shared, as @PGriffith suggested and now I get an error every time that says ‘no module named shared’ or ‘object has no attribute mes’. My error message before adding import shared simply stated ‘global name shared not defined’:

else:
	#import shared
	results = shared.mes.analysis.getProductionHistory(eqPath, startDate, endDate)
	pyResults = system.dataset.toPyDataSet(results)

This is becoming quite a nuisance and I am hoping there is either a reason or workaround for it that I just haven’t found in the forum yet.