We have GP tools in GP toolbox OurServices.tbx and some of them are written in Python. We access them via geoprocessing services. However some Python functions are just copy-pasted from one GP function script to another.
The question is: Is it possible to embed a Python script, say our_utils.py, into the toolbox file (tbx) so in all our Python tools we are able to access shared functions like this:
import our_utils
our_utils.sharedFunc()
We don't want pack shared functions as separate GP tools and then use ModelBuilder; this approach is too heavyweight for the issue.
Bounty edit 1 (2015-05-05)
I feel I have to give a bounty to someone participating here. At least three beautiful minds were attracted by it so I don't want my bounty going to dust.
I don't have ArcGIS at my fingerprints at the moment but I've spent 30 to 40 mins today checking if @mr.adams hacks can be extended to server installations (geoprocessing rest services). And, surprisingly, it worked with some additional hacks!
The hacks were: after packaging to .sd file (still a mystery for me how to pack more than one tool) rename that file to .zip and add libraries folders (import mylib => you must add mylib folder into zip). Then rename .zip back to .sd.
So my modest bounty goes to @mr.adams. I won't mark his answer as true answer; I still need to find a simpler way.
For pure Pythonistas @Farid_Cher gave an excellent .pyt template. I hope many copypasters will use it.
*.tbx
) or a Python toolbox (*.pyt
) of tools. You can doimport our_utils ... our_utils.doSomething()
with both, but your question asks about embedding that leaves me wondering which you are using. Also, what version of ArcGIS for Desktop are you using? Can you edit your Question to revise it with these details, please?sys.path.append('C:/Working/SomeFolder')
before your import. If the linked Q doesn't answer your question please flag for reopening (and explain how it doesn't fit).