Select to view content in your preferred language

How do we import a script that defines a function using arcpy without a NameError?

2389
10
07-27-2017 05:08 PM
MichaelMorgan4
Regular Contributor

I am trying to run a script called update_parcels.py in the Arcmap python window (version 10.4) that imports another script in the same directory that defines field mappings (it has a single def statement). My understanding is that both scripts should import arcpy, because both are using arcpy functions (field map objects and management geoprocessing methods), but running the first script results in an error: "NameError: global name 'arcpy' is not defined," as it imports the second script.

Should just 1 script import arcpy? Should I set PYTHONPATH in the OS (Windows 10)? What kinds of things should I be looking at to be able to use the function definition in the second script in my first ("main") script? Thanks.

0 Kudos
10 Replies
ClintonDow1
Frequent Contributor

By chance are you importing arcpy in a executable context like this:

if __name__ == '__main__':
  import arcpy

Its a commonly suggested idiom to be able to run scripts from the command prompt, however it causes problems if run from within ArcGIS since 'arcpy' won't be defined in namespaces outside of the conditional. Its a shot in the dark but seems possible here. 

0 Kudos