NameError ( name "logout" is not defined) - strange?

Asked by BrazilMac on 2018-09-13

Here is my script:

--------------------

wait("1536669464524.png")
click("1536669473041.png")
click("1536669473041.png")
wait("1536669757342.png",30)
if find("1536669757342.png"):

    #Use pre-defined hotkeys on api app
    type('u', KeyModifier.CTRL)
    wait(1)
    type('c', KeyModifier.CMD)
    username = Env.getClipboard()

    type('p', KeyModifier.CTRL)
    wait(1)
    type('c', KeyModifier.CMD)
    password = Env.getClipboard()

    type('s', KeyModifier.CTRL)
    wait(1)
    type('c', KeyModifier.CMD)
    search = Env.getClipboard()

    wait("1536657404192.png")
    click("1536657070381.png")

    click("1536657070381.png")

    wait(1)
    type(username)
    type(Key.TAB)
    wait(1)
    type(password)

click("1536657336512.png")
if exists("1536672253666.png") :

    print('PASS IS INCORRECT')
    type(Key.ESC)
    wait(1)
    click("1536687573573.png")
    click("1536687573573.png")

else :

    wait("1536671840980.png")
    while exists(Pattern("1536683293833.png").similar(0.90), 0):

        click("1536671852025.png")
        click("1536671852025.png")
        click("1536671878418.png")
        wait(1)
        type(search)
        wait("1536675224487.png")
        click("1536675224487.png")
        wait(1)

        if exists("1536685239396.png") :

            print('ACCOUNT INVALID')
            logout()
            click(Pattern("1536687625649.png").similar(0.79))
            click(Pattern("1536687625649.png").similar(0.79))

        else:
            click("1536677027804.png")
            wait(1)
            type(Key.ESC)
            wait(10)
            click(Pattern("1536676724489.png").targetOffset(-18,-1))
            click("1536675568133.png")
            click("1536675568133.png")
            wait(1)
            type('s', KeyModifier.CTRL)
            wait(1)
            type('c', KeyModifier.CMD)
            search = Env.getClipboard()

    logout()
    click("1536689430382.png")
    click("1536689430382.png")

def logout():

    click("1536684131754.png")
    click("1536684131754.png")

    click("1536683889988.png")
    click("1536683905637.png")
    click("1536683920971.png")

-------------------------------------------

It was working fine, but for some reason this message started popping up. any clues?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
BrazilMac
Solved:
2018-09-14
Last query:
2018-09-14
Last reply:
2018-09-14
BrazilMac (isoripper) said : #1

If it helps, Im developing this on OSX, and testing on a linux machine on mac, the error does not come up and the function logout() executes as it should

masuo (masuo-ohara) said : #2

Function logout() should be defined before logout() is called.
Your codes is similar to code1.
Change code1 to like code2 or code3, it will work.

[code1]
logout()

def logout():
    print "logout"

[code2]
def logout():
    print "logout"

logout()

[code3]
def main():
    logout()

def logout():
    print "logout"

main()

BrazilMac (isoripper) said : #3

HI masuo thanks for your reply.

It has worked before, so thats why Im puzzled...on osx environment it works fine, when defined at the end of file. On linux, there are times that it works, but most of the time it doesnt...

RaiMan (raimund-hocke) said : #4

Masuo's answer is correct: the function-def logout has to be moved to the top.

The symptoms you tell about can only happen inside the IDE, where you run the same script more than once.

The logout is referenced in an else branch. If this is not visited at the first run, then the function-def will be collected at the end of the script and will be defined for each subsequent run.

The reason behind: While in the IDE, you have only one interpreter session, that remembers the names already defined.

Best practice: Make final tests of a script by running from commandline, where a script is only run once with a new interpreter session every time it is run.

BrazilMac (isoripper) said : #5

Thanks RaiMan, that answers my question!