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

1288
10
07-27-2017 05:08 PM
MichaelMorgan4
New Contributor III

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
RhettZufelt
MVP Frequent Contributor

Try not importing arcpy in either of them.

It is not required when running in the ArcMap python window.

R_

0 Kudos
MichaelMorgan4
New Contributor III

My main script is using TruncateTable_management and Append_management, which are both methods coming directly out of arcpy, so the main script needs to import. I am now trying to run the secondary (def function) script first in the Arc Python window, also with arcpy to get FieldMapping() and FieldMap() objects, to see if this just retains the function definition in this Arc session.

0 Kudos
RhettZufelt
MVP Frequent Contributor

You said you are running in the "ArcMap" python window.  Arcpy is automatically "imported" into that, so no need to import it again.

Typing/entering "import arcpy" into the ArcMap python window has absolutely no affect at all, so why bother, especially if it is giving you problems.

R_

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

How exactly are you importing this files, and what do the import statements look like in each file.  I don't have a problem importing a file that references arcpy along with another file/module that also imports arcpy.

0 Kudos
MichaelMorgan4
New Contributor III

It looks like I can get around this problem by loading the def function script first (importing arcpy for field map objects), and then loading the main script, also importing arcpy but not importing the secondary script. Thanks all.

0 Kudos
RhettZufelt
MVP Frequent Contributor

Is there a reason you need to import the def function.  You can just copy/paste it into the main script unless the import is needed for something else.

R_

0 Kudos
MichaelMorgan4
New Contributor III

Really just to understand how this process works, but also because the def function should stand alone and be able to be called from other scripts as needed. This is my first script that needs to call a function, but it is probably not my last.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Going back to my first comment:

How exactly are you importing this files, and what do the import statements look like in each file.  I don't have a problem importing a file that references arcpy along with another file/module that also imports arcpy.

If you haven't already, I suggest you read 6. Modules — Python 2.7.13 documentation  , it has lots of helpful information on importing modules, which is really what you need to focus on since your function lives within a module.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

a cheesie example... but you will get the drift I hope

# -*- coding: UTF-8 -*-
"""
Script: main_script.py
Requires: amazorama.py

"""
import arcpy
import amazorama as amaze

a = "some string that I am going to use a function"
b = amaze.some_func(a)
print("From beyond...{}".format(b))

# ----------------- save the above code as main_script.py --------------
# ----------------- save the below code as amazorama.py ----------------
# now, save both in the same folder, and load, check and run main_script

# -*- coding: UTF-8 -*-
"""
amazorama.py

imported by main_script.py
"""
import arcpy
print("Just visiting while I import the functions from this script")
print("Arcpy is located in {}".format(arcpy.__file__))


def some_func(a):
    """does stuff"""
    if a is str:
        return(a.upper())
    else:
        return(str(a).upper())

results

runfile('C:/Temp/main_script.py', wdir='C:/Temp')

Just visiting while I import the functions from this script
Arcpy is located in C:\ArcPro\Resources\ArcPy\arcpy\__init__.py
From beyond...SOME STRING THAT I AM GOING TO USE A FUNCTION
0 Kudos