3

First, I'm trying to call a function from a module (final_functions) I created in a separate script. However I receive the following error:

Traceback (most recent call last):   File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__   File "C:\EsriPress\Python\Data\Final\Test.py", line 8, in <module>
    check = final_functions.FieldExists(fcname, fieldname)   File "C:\EsriPress\Python\Data\Final\final_functions.py", line 6, in FieldExists
    namelist = [] NameError: global name 'fc' is not defined

Here's the main script:

import arcpy
from arcpy import env
arcpy.env.workspace = "C:/EsriPress/Python/Data/Final/Scratch.gdb"
import final_functions
fclist = arcpy.ListFeatureClasses('Fire*')
for fc in fclist:
    fc = fcname
    fieldname = "SFCount"
    check = final_functions.FieldExists(fcname, fieldname)
    print check

Here is the module (final_functions.py) I'm importing:

def FieldExists(fcname, fieldname):
    namelist = []
    fields = arcpy.ListFields(fcname)
    for field in fields:
        namelist.append(field.name)

    if fieldname in namelist:
        return "Field: SFCount already present in Feature Class:     {0}".format(fcname)
    else:
        arcpy.AddField_management(fcname, fieldname, "TEXT")

if __name__ == "__main__":
    fclist = arcpy.ListFeatureClasses('Fire*')
    for fc in fclist:
        fieldname = "SFCount"
        fcname = fc
        checkfields = FieldExists(fc, fieldname)
        print checkfields

When I run final_functions on its own, it works great. I'm only given the error when trying to call it in the main script.

And if you're wondering, I'm just checking a list of feature classes for the existence of a field, and adding a new field if it doesn't exist.

0

2 Answers 2

8

The reason it works inside your module is that you are passing each feature class (fc) into your function.

If you look carefully you can see that the line:

fcname = fc

is redundant. Here you are essentially reassigning the feature class to the variable fcname and then not using it i.e.

checkfields = FieldExists(fc, fieldname) # you are still passing fc to your function

As for your main script, you are looping through each feature class as fc but then saying:

fc = fcname

however fcname is not defined. So you could create the variable fcname by assigning it to your feature class inside your loop (as you have done inside your module), and ensure you then pass fcname into your function:

for fc in fclist:
    fcname = fc # create new variable to store feature class (unnecessary)
    fieldname = "SFCount"
    check = final_functions.FieldExists(fcname, fieldname) # new variable passed to function
    print check

but this is unnecessary as you already have a variable that is storing your feature class - fc. So, just remove this reassignment and pass fc into your function. Therefore I would change your main script to:

import arcpy
from arcpy import env
arcpy.env.workspace = "C:/EsriPress/Python/Data/Final/Scratch.gdb"
import final_functions
fclist = arcpy.ListFeatureClasses('Fire*')
for fc in fclist:
    fieldname = "SFCount"
    check = final_functions.FieldExists(fc, fieldname) # fc goes straight into function
    print check

and inside your module I would change the if name...: section to:

if __name__ == "__main__":
    fclist = arcpy.ListFeatureClasses('Fire*')
    for fc in fclist:
        fieldname = "SFCount"
        checkfields = FieldExists(fc, fieldname) # fc goes straight into function
        print checkfields
1
  • @AndyFisher Glad to help. If you are happy that this solved your issue could you please accept it as the answer.
    – Ali
    Apr 28, 2016 at 7:52
7

In your main script you have switched a couple of parameters around. In the first line of your for loop you have fc = fcname and it should read fcname = fc

import arcpy
from arcpy import env
arcpy.env.workspace = "C:/EsriPress/Python/Data/Final/Scratch.gdb"
import final_functions
fclist = arcpy.ListFeatureClasses('Fire*')
for fc in fclist:
    fcname = fc
    fieldname = "SFCount"
    check = final_functions.FieldExists(fcname, fieldname)
    print check

Changing these around should fix the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.