2

I have two ArcGIS python scripts. One is a primary python script that calls a number of functions from a second python script. Everything works well, i.e. primary tool calls and uses functions from a second script, when the two scripts are placed in the same directory and the primary python script is NOT embedded into python toolbox. However when the primary python script gets embedded into ArcGIS toolbox, the second script cannot be access any more by the primary script and therefore cannot be imported into the primary script as a module and functions cannot be used.

The work around would be to copy over all functions from the second script to the primary script or to copy second script into the toolbox directory but that is not the proper way to solve this issue.

Is it possible within ArcGIS to embed a script into a python toolbox with other scripts that are used by the primary script?

  • What version of ArcGIS for Desktop are you using? And are you using Python Script Tools or Python (Only) Toolboxes? – PolyGeo Oct 30 '13 at 9:07
  • It is ArcGIS 10.0 and I am using Python Script Tools added to the Toolbox. – Kasia Oct 30 '13 at 9:22
  • Are you able to post some simple test code to illustrate your coding pattern and the steps you have tried to get this working. The only part that I have a question mark over is the embedding so I thing making that the only variable in two otherwise identical workflows should highlight whether that is the issue. – PolyGeo Oct 30 '13 at 9:35
  • It is what I mean by embedding: resources.arcgis.com/en/help/main/10.1/index.html#//…. Primary script imports the second script as a module: "import secondScriptName" and the primary script calls the function from within the second script: secondScriptName.functionName() – Kasia Oct 30 '13 at 9:57
  • I gathered that - I was hoping you could provide some test code that I could quickly test rather than having to write from scratch. – PolyGeo Oct 30 '13 at 10:06
5

This is an issue with how python works. When you import a module, it will search for it in the built-in modules, the paths in sys.path and finally, the current working directory (Python Module Search Path).

You've already noticed that adding your second script to the same directory as the toolbox will allow your first, embedded script to import it. This is because you've put it in the working directory. Your other options are making it available to python in other ways (detailed in the docs I linked): Modifying sys.path from within the embedded script or creating/modifying the PYTHONPATH environment variable.

My guess is that ArcGIS simply stores the embedded python as text and executes it when needed. It has no way of knowing dependencies or what extra modules are imported. You would have this same problem if you used other 3rd party modules that were not part of the default installation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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