← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #172705]: NameError: global name 'XXX' is not defined since reload is used instead of execfile

 

Question #172705 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/172705

    Status: Open => Answered

RaiMan proposed the following answer:
not really clear, where is what.

just guessing:

--- main.sikuli

import IPS
reload(IPS)
from IPS import *

# if __name__ ... only needed, if main.sikuli might be imported by other .sikuli's 
snap = Output(snapOutput,0)
usual = Usual()
os.system('\"c:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" "http://127.0.0.1:80";')
usual.open_Webstart()

--- IPS.sikuli
from sikuli import *

class Usual():
    def open_Webstart(self):
        # code
        snap.take_screen_shot()

The problem is, that the name snap is only global in the main context and when using import, the globals of main are not known (and cannot be made accessible) in the context of an imported module. 
(This is the reason, why you have to add from sikuli import * in imported modules, though it is already imported (automatically at start) in the main script (the script that is run in the IDE)).

When using exec file(), the executed code is evaluated in the context of
the script where it is run (just as if the code was written down at this
place), so all globals in main are known.

The fact, that this does not work anymore, shows that you overall
structure is not appropriate for modularization.

To adjust your problem with little effort:

--- IPS.sikuli
from sikuli import *
class Usual():
    def __init__(self, snap):
        self.snapGlobal = snap

    def open_Webstart(self):
        snap = self.snapGlobal
        # code
        snap.take_screen_shot()

--- main.sikuli
import IPS
reload(IPS)
from IPS import *

# if __name__ ... only needed, if main.sikuli might be imported by other .sikuli's 
snap = Output(snapOutput,0)
usual = Usual(snap)
os.system('\"c:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\" "http://127.0.0.1:80";')
usual.open_Webstart()

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.