Search code examples
pythonnamespacesdefinedpyautogui

NameError: name 'VAR' is not defined


I have a Python script here that is for figuring out the dimensions of a work space to then figure out where the automation script should be clicking or inputting data.

I am running into a problem with a variable have made in one function but isn't accessible in another. I returned the variable I'd like to continue working with through the script but it is giving me an error saying it has not been defined.

Here is my code thus far :

import pyautogui
import time
import finder

def areaSetup():
    getWorkSpace('img/dimen/loginDimTop.png' , 'img/dimen/loginDimBot.png')
    getWaHWorkSpace(workSpace)
    getQuad1(workSpace,W, H, Hh, Wh)
    getQuad2(workSpace,W, H, Hh, Wh)
    getQuad3(workSpace,W, H, Hh, Wh)
    getQuad4(workSpace,W, H, Hh, Wh)
    return workSpace, quad1, quad2, quad3, quad4

def getWorkSpace(browTopLeft, browBotRight):
    top = pyautogui.locateOnScreen( browTopLeft , grayscale=True)
    print (top)
    bottom = pyautogui.locateOnScreen( browBotRight , grayscale=True)
    print (bottom)
    x1, y1, h1, w1 = top
    x2, y2, h2, w2 = bottom
    x2 = x2+w2
    y2 = y2+h2
    print ("initial print" , x1, y1, x2, y2)
    workSpace = x1, y1, x2, y2
    print ("Workspace" , workSpace)
    return workSpace

def getWaHWorkSpace(workSpace):
    x1, y1, x2, y2 = workSpace #break it into four parts again
    W = x2 - x1 #get the height
    H = y2 - y1 #get the width
    Hh = 0.5 * H #get mid point of height
    Wh = 0.5 * W #get mid point of width
    print ("W and H" , W, H, Hh, Wh)
    return W, H, Hh, Wh

My traceback is the following :

(14, 40, 42, 47)
(694, 774, 342, 44)
initial print 14 40 738 1116
Workspace (14, 40, 738, 1116)
Traceback (most recent call last):
File "loginPage_001.py", line 35, in <module>
areaDimensions.areaSetup()
File "/home/tvorac/python/testBot/areaDimensions.py", line 7, in areaSetup
getWaHWorkSpace(workSpace)
NameError: name 'workSpace' is not defined

I am fairly sure I am overlooking something simple and will continue looking through the Python documentation to see if I can catch what I am doing wrong.

Thank you for your time regarding this!


Solution

  • The problem is that Python has a variable scope much like any other programming language.
    Meaning that depending on where you put the variable, it might only be accessible within that scope, in this case your function getWorkSpace() that creates it.

    In order for everyone else to access it you have to do one of two things.
    Either create a global variable, much like this:

    import pyautogui
    import time
    import finder
    
    workspace = None
    
    def areaSetup():
        global workspace
        ...
    
    def getWorkSpace(browTopLeft, browBotRight):
        global workspace
        ...
        workSpace = x1, y1, x2, y2
    
    def getWaHWorkSpace(workSpace):
        global workspace
        ...
    

    Or simply use the returned value from your function.

    workspace = getWorkSpace('img/dimen/loginDimTop.png' , 'img/dimen/loginDimBot.png')
    

    Because getWorkSpace() sure enough returns a workspace just as you would expect it to, since you've done return workSpace, the problem is that you doesn't store it anywhere.

    These are your two options, either handle the return value and use the getWorkSpace function as intended. Or create a global variable