global variable not defined

Posted on
Tue Aug 23, 2016 12:46 pm
cramer offline
Posts: 33
Joined: Feb 03, 2015

global variable not defined

I am running a python script file from a Virtual on/off device.

I declare a variable at the beginning of the script file - LOG. I declare it as a global at the start of a method (def) but when the method is called, I get a global variable not defined error. When I run the script within PyCharm, it works ok.

As per another forum message, I have tried stopping / starting the server.

Any ideas? I am new to Python so it it is possibly a newbie mistake but I have tested extensively in PyCharm.

cutdown script code :

import statements...

LOG = True

def log(_text):
global LOG
if LOG == False:
return
... code ...

def mainprocess():

log("Running mainprocess"

... code ...

mainprocess()

Posted on
Tue Aug 23, 2016 12:51 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: global variable not defined

This forum post explains what is occurring and has a couple of workarounds.

Image

Posted on
Tue Aug 23, 2016 1:37 pm
cramer offline
Posts: 33
Joined: Feb 03, 2015

Re: global variable not defined

Thanks Matt. I might try a slightly different approach by defining a class and declare my globals as class variables. Then i can use classname.globvarname. If this works, it seems a reasonably tidy approach.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests