1

I'm writing a script that calls a function I wrote in another script. I can't access the env path python is installed on so I must save my scripts in another location. To get one script to call another, I have to use sys.path.append(r"pathToPythonFiles") and then import the script as a module and then call the function.

Main script:

import sys
import arcpy
import os


sys.path.append(r"N:\Common\Script\Drivers")

import CO_181_ExStMetal as sMetal

sMetal.exportMetal()

script with callable function:

def exportMetal():


    # Create program variables

    # workspace
    mxd = arcpy.mapping.MapDocument(r"C:\RS_Data\Workspace\Batch_Carts_CO_181\Batch_Carts_CO_181.mxd")

    # dataframe
    df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

    # field for SearchCursor
    numField = 'Route'

    # feature layer for cursor
    lyr = arcpy.mapping.Layer(r"C:\RS_Data\Workspace\Batch_Carts_CO_181\gisdb\layers.gdb\Service_Location")

    # create empty set to hold unique route values
    rtVals = set()

    # Create searchCursor
    cursor = arcpy.SearchCursor(lyr)
    for row in cursor:
        rtVals.add(row.getValue(numField))

    scenario = max(rtVals)

    path = r"S:\Shared\RouteSmart\Automated_BIC_and_Carts\CO_181\Standish_Yard\Metal"

    outNameCarts = os.path.join(path, ("Metal_Report.pdf"))
    print outNameCarts

    rlfCarts = r"N:\Common\Report Templates\CO_181_Metal.rlf"

    reportCarts = arcpy.mapping.ExportReport(lyr,rlfCarts,outNameCarts, "USE_RLF")

if __name__ == '__main__':

    exportMetal()

Here is the error:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 654, in run
    exec cmd in globals, locals
  File "N:\Common\Script\Drivers\sys_test.py", line 1, in <module>
    import sys
  File "N:\Common\Script\Drivers\CO_181_ExStMetal.py", line 10, in exportMetal
    import arcpy
NameError: global name 'arcpy' is not defined

I've tried import arcpy at the top of script 2 before defining exportMetal() as a global and then locally within the function as well. I also tried after if name=='main':

I'm concerned that once I set the system path in the script the program can't find arcpy which is installed in the python env path.

  • What happens if you import arcpy above the def exportMetal(): line? Any module/package that is used within a module must be imported somewhere within that module. – Tom May 2 '18 at 21:41
  • @Tom same result – geoJshaun May 2 '18 at 22:18
  • So it's not that the import fails but you still get the NameError? Can you post your full code and the full error message for that? – Tom May 2 '18 at 23:11
  • What happens if you create a new script with just import arcpy and run that? – Midavalo May 3 '18 at 1:27
  • 1
    You need to import arcpy in your callable script – Ben S Nadler May 3 '18 at 5:16
0

This isn't really an ArcGIS arcpy issue. You'll get the same error if you try to use any python module/package that you haven't imported (into the correct scope).

You need to import arcpy at the top of your module (or at the top of any script that uses the arcpy package)

i.e

# Test.py
import sys

sys.path.append(r"N:\Common\Script\Drivers")
import CO_181_ExStMetal as sMetal
sMetal.exportMetal()

# CO_181_ExStMetal.py

import arcpy   # <--- import here

def exportMetal():


    # All your code

    # Just to show it works:
    print(arcpy.ProductInfo())
    print(arcpy.GetInstallInfo()['ProductName'])

if __name__ == '__main__':
    exportMetal()

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