Global name 'system' is not defined

In my gateway event script, I have the following code:

def endTimer(addTime):

today = system.date.now()
unformatTime = system.date.addMinutes(today, addTime)
formatTime = system.date.format(unformatTime, "HH:mm")

return formatTime

But I keep getting the following error: global name 'system' is not defined

What am I doing wrong?

Try adding import system to the first line of the code.

1 Like

Nothing - this is a holdover from legacy scripting. You can call import system inside your defined function and run the system functions correctly. This (should) only apply to defined functions inside gateway event scripts.

1 Like

Gateways scripts still use the old legacy Python variable scoping. It might be that. Try adding “import system” to your function.

4 Likes

That worked. Thanks guys!

This problem just cropped up for me on Ignition 8.0.2.
It’s easily solved by doing: import system
But I’ve never had to do it before today using several prior versions of Ignition
Gateway event scripts are working without it, as recently as 30 minutes ago but now it is suddenly needed? Very strange


I have the same error with the gateway event script - Tag Change below :

import StringIO
import csv
import os
import ftplib
import sys
import system
from datetime import date
from datetime import datetime
from datetime import timedelta

if newValue.value==0:
	sys.exit()

today=date.today() #RĂ©cupĂšre la date du jour au format 'aaaa-mm-jj'
datejour= str(today).replace('-','') #Remplace les - par rien
yesterday=date.today() - timedelta(days=1) #RĂ©cupĂšre la date de la veille
yesterday_jour=str(yesterday).replace('-','') #Remplace les - par rien
#print datejour,yesterday_jour


ftp = ftplib.FTP('myip') 
ftp.login('user','password')
files = [] #créé liste vide
ftp.retrlines('NLST', files.append) #RĂ©cupĂšre la liste des fichiers dans le dossier du FTP et alimente la liste

#print files

def callback(buf):
	ftmp=system.file.getTempFile("data")
	#print ftmp
	
	f=open(ftmp, 'wb')
	f.write(buf)
	system.file.writeFile("C:\\TempTest\\NewTest.data",buf)
	#print buf
	
	dataIn=	StringIO.StringIO(buf)
	reader=csv.reader(dataIn, delimiter=';', quotechar="\"")
	dataOut=[]
	headers=["Poste","Nom","Date","DJU"]
	for row in reader:
		dataOut.append(row)
	#print dataOut
	
	date=datetime.strptime(dataOut[-1][2]+'000000','%Y%m%d%H%M%S')
	dju=dataOut[-1][3].replace(',','.')
	#print dataOut[-1][2], dju,date
	
	
	f.close()
	
	query="INSERT INTO dju (t_stamp,dju) VALUES (?,?)"
	system.db.runPrepUpdate(query,[date,dju])


filename="DJU_"+yesterday_jour+".data"
#print filename
ftp.retrbinary('RETR '+filename, callback)
ftp.voidcmd('QUIT')
ftp.close()

Thanks

I tried, but it not work. It still shows ‘system’ is not defined.



ignition 8.1.10

Try putting import system IN your function, i.e.

def foo():
   import system
   system.Do.stuff


2 Likes

That’s right.

1 Like

Better, move all of that script to a project script module function. The legacy scoping issue where def doesn’t pick up the normal pre-defined imports only happens in the script in the event editor. It doesn’t happen in project script modules.

3 Likes