I keep getting an error that says that my variable (region) is not defined. Please see the code below:
# Set local variables mapName = str(mxd.filePath).split('\\')[-1:][:-4] region = mapName[:2] inTable = nodeFeatures inField = "CUSTRegion" expression = "Reclass (!CUSTNodeID!, !CUSTRegion!)" codeBlock = """def Reclass (CUSTNodeID, CUSTRegion): if CUSTNodeID != None: return region else: return None""" # Execute CalculateField arcpy.CalculateField_management(inTable, inField, expression, "PYTHON_9.3", codeBlock)
The variable "mapName" grabs the name of the map and turns it into a string. The map name has 4 parts REGION - STATE - PROJECT - CUSTOMER. The variable "region" takes the mapName variable and grabs the first two characters, which is the abbreviated region code ("NC", "NE", etc...)
I originally had these two variables at the top of the script just under where I import the modules, but I thought that I would move them down closer to the process that uses them hoping it would help (I'm still pretty green with Python so I'm not even sure if it matters where the variable is located). I put it at the top to begin with because there are A LOT of process in the whole script that use the same variables.
I can run this script in the Python window just fine, but when I create a python tool in a toolbox, I keep getting the error that says that the variable "region" is not defined. I've tried it with replacing "region" with "mapName[:2]" and that just returns saying that "mapName" is undefined. I tried using "str(region)" and "str(mapName[:2])" to no avail.
Do I need to move the variables somewhere or change some sort of defining operator or something?