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.
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") # 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.