Publishing dependencies with custom tool to server

1038
5
09-21-2012 06:00 AM
GregFinch
New Contributor
I have a custom script tool (python) that uses some classes defined in separate .py files.  When I publish the tool to ArcGIS server only the script is deployed, but its dependencies are left behind.  Is there any way to package python dependencies with a tool so they are deployed with the tool?  I'm using version 10.1.
Tags (2)
0 Kudos
5 Replies
KevinHibma
Esri Regular Contributor
Yes, see this topic: http://resources.arcgis.com/en/help/main/10.1/#/Authoring_geoprocessing_tasks_with_Python_scripts/00...
Specifically the "Importing other Python modules" section
0 Kudos
GregFinch
New Contributor
Thanks for the quick response Kevin.  That article is very helpful.  However, it doesn't seem to be working as it's described.  The script I'm trying to publish looks like this:

import arcpy
from ADSSurface import ADSSurface

fieldId = arcpy.GetParameterAsText(0)
year = arcpy.GetParameter(1)

surface = ADSSurface(fieldId, year)
raster = surface.getRaster()

arcpy.SetParameter(2, raster)


ADSSurface.py is in the same folder as this script (GetSurface.py) and it contains all of our custom logic.  After I publish and look in the server directories, the published script (GetSurface.py) is identical to the original script and when I run on server, I get an error something like "ADSSurface not found."  There is no sign of ADSSurface.py embedded or standalone on the server.

Is there something that controls whether the published script embeds its dependencies or not?  Anything that could prevent the embed from happening?  ADSSurface uses a 3rd party site-package that is installed on the server.  Would that cause ArcGIS to not embed dependencies?
0 Kudos
KevinHibma
Esri Regular Contributor
Well we dont scan any imported scripts, so the fact it imports a 3rd party module wouldnt have any impact on it.
Based on your explanation and code, this should be working. Nothing you've said stands out as strange to me.
Well - the published script you're looking at inside the arcgisinput directory: it doesnt have at the very least a new header of #esri added variables#? I'd expect to see at least that.


Would you be willing to re-work your script ever so slightly to see if this works?
Make a folder at the same level as your ADSSurface.py called "scripts"
Move your GetSurface.py script in there. Then add the sys.path.append into your script (shown below)
I know that this will copy over everything in the "myPythonModules" folder.
Because of sys.path.append, when it gets moved to the server, the paths will line up.

import arcpy, os, sys
myPythonModules = r'e:\<directory where ADSSurace.py lives>\Scripts'
sys.path.append(myPythonModules)

from ADSSurface import ADSSurface

fieldId = arcpy.GetParameterAsText(0)
year = arcpy.GetParameter(1)

surface = ADSSurface(fieldId, year)
raster = surface.getRaster()

arcpy.SetParameter(2, raster)
0 Kudos
GregFinch
New Contributor
Thanks Kevin!  Your suggestion worked.  It actually prompted me when publishing that it was copying the scripts folder and did move it and update the original script correctly.

One other thing I found, in my original script, I had a few imports to modules from the 3rd party site-package I mentioned.  Those modules weren't used in the script, they just hadn't been removed from the import.  When I take out the 3rd party imports and publish, then ADSSurface.py is copied to the server.  When I add back the 3rd party imports and publish, ADSSurface.py is NOT copied.

In either case, if I follow your suggestion using sys.path.append, then ADSSurface.py is copied over and everything works.
0 Kudos
KevinHibma
Esri Regular Contributor
Ok cool. I'm glad you're off and running.

And thanks for the info - I'll use it here to see if I can reproduce whats happening to better understand why its not behaving like it should.
0 Kudos