6

I am writing a lengthy ArcPy tool and am working with multiple python scripts, simply for organization's sake. Everything runs well when I run it from the command line, but now that I am starting to think about converting it to a script tool, I am getting a headache.

My previous structure:

  • parameters.py : lists all globals (parameters, functions, variables)

  • a_functions.py, b_functions.py, etc. : lists functions for use in main code, imports parameters with *

  • maincode.py : calls functions from *_functions.py, imports parameters with *

The structure is quite neat, and although it may not be ideal from a file structure standpoint, it works well enough (chime in if I am doing something horribly wrong).

Now that I need to load a file into the script tool that defines the parameters, I can't simply move all the parameters to the main code file, as I need to import them as globals to the other files, and importing with * will also import all the globals local to maincode.py.

I could just move everything into one file, but all the organization would be lost and it would be a big mess.

Does anyone have any suggestions who has built a lengthy arcpy tool with multiple files before?

4
  • Does this Q&A help? gis.stackexchange.com/questions/11339/…
    – PolyGeo
    Commented Dec 7, 2012 at 1:40
  • 1
    Or how about this: Guidelines for organizing Python Toolboxes (.pyt) in ArcGIS Commented Dec 7, 2012 at 3:52
  • Thank you. Actually, the ESRI blog post concerning Python Templates is exactly what I'm looking for. My particular problem was not importing modules, but deciding on a good file structure for my code. Much obliged! Commented Dec 7, 2012 at 16:39
  • Spoke too soon. The ESRI blog post concerning Python Templates is on the right track, but doesn't address multiple files. Ideally, I am looking for an example of a parameters.py that defines all params and global variables, and also imports the other modules. These other modules will need to import the globals from parameters.py in order to reduce the number of inputs to their functions. The problem I have is multiple imports occurring at once, and I'm convinced there's a better way. Commented Dec 7, 2012 at 17:09

1 Answer 1

5

Use a package structure. __init__.py can hold your package scope variables. The package modules can access those variables easily, calling scripts can set them if they need to and everything is all nicely wrapped up in a single directory.

e.g.

#  |  mainscript.py
#  \--somepackage
#     |  __init__.py
#     |  script1.py
#     |  script2.py

#----------------------------
#mainscript.py
#----------------------------
import somepackage

#set some variables
somepackage.args=['a','b','c']
somepackage.kwargs={'d':1,'e':2,'f':3}

#Call some functions
somepackage.script1.main()
somepackage.script2.main()

#----------------------------
#somepackage.__init__.py
#----------------------------
__all__=['script1','script2']
import script1,script2

#Some defaults
args=[]
kwargs={}
somevar=1
anothervar=2

#----------------------------
#somepackage.script1.py
#----------------------------
import somepackage
def main():
    print somepackage.args
    print somepackage.somevar

#----------------------------
#somepackage.script2.py
#----------------------------
import somepackage
def main():
    print somepackage.kwargs
    print somepackage.anothervar
6
  • I agree that this is the best way to organize a collection of scripts, but the complication with an arcpy script tool is that the file specifying the global variables (i.e. parameters) must also execute the main code. Commented Dec 10, 2012 at 16:43
  • I'm not sure what the problem is. Can you explain a bit more clearly what you need to do as my example basically replicates the process in your question. mainscript.py runs the code and somepackage.__init__.py holds all the "global" variables.
    – user2856
    Commented Dec 10, 2012 at 20:31
  • How do you run your original set of scripts - do you call parameters.py or maincode.py from the command line?
    – user2856
    Commented Dec 10, 2012 at 20:50
  • The problem is that a single script gets loaded into the "script tool", which gets a GUI and is able to be run from an ArcGIS toolbox. This script must contain references to the parameters and must also run the process. Therefore, a "parameters.py" file would need to run the main code as well. I got around this eventually by using [[if _ name _ == '_ main _':]] to separate the globals from the call to the main functions and other variable definitions. Is there a better, more elegant way? Commented Dec 12, 2012 at 19:16
  • i will choose this as the answer, because it is correct, but preface that it will not work for arcpy script tools. in this case, please put all your executables and globals you'd like to keep local your main script below if __name__ == '__main__': Commented Dec 20, 2012 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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