Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

For one of my tasks I need to create a Toolbox with three tools (python scripts).

Instead of ordinary tbx I've decided to write it as Python Toolbox (pyt).

Everything is clear except the "good" way to organize the code.

As I have three tools it is not good way to store them in one file (pyt). So, I've decided to keep each tool in a separate .py file.

Here is a problem: what is the good way to organize several files with PYT for distribution or deployment on ArcGIS for Server? Should I keep them on the same level with PYT file or should I place them in some subdirectory (i.e. "Tools")?

Can you recommend any Esri guidelines or reference "big PYT toolbox" sample?

I have not find anything on this topic. In version 10.0 there was so called ToolShare folder structure which I've used.

This is more a question of style of coding. Because the idea to create one PYT file with 500-1000 or more lines of code does not look good to me and I believe it is not "pythonic" way.

Thanks!

share|improve this question
    
"the idea to create one PYT file with 500-1000 or more lines of code does not look good to me". U're not alone, Alex. Today I promptly stated that .pyt is an invention of sick mind. And I won't regret it. –  r.pankevicius Mar 7 at 22:04
add comment

1 Answer 1

up vote 8 down vote accepted

Have a look at this thread on the ArcGIS forum. Basically just use standard python modules or a package structure and import your tools into the python toolbox.

Something like:

#  \--SomeDir
#     |  toolbox.pyt
#     \--toolpackage
#        |  __init__.py
#        |  script_a.py
#        |  script_b.py


#----------------------------
#The .pyt file
#----------------------------

import arcpy
from toolpackage.script_a import Tool1
from toolpackage.script_a import Tool2
from toolpackage.script_b import Tool3

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "MultiTool_Toolbox"
        self.alias = "Toolbox with Multiple Tools"

        # List of tool classes associated with this toolbox
        self.tools = [Tool1, Tool2, Tool3]
share|improve this answer
    
I've found that this works ok in ArcGIS Desktop, but when publishing a geoprocessing service to an ArcGIS Server instance then it loses all of the scripts and packages, apart from the .pyt file. It might be possible to drop the scripts into the deployment manually, but that increases complexity. –  Gnat Jun 20 '13 at 6:42
    
@Gnat - see the "Importing other Python modules" section in the ArcGIS Server help. Maybe the from toolpackage.script_a import Tool1 needs to be replaced with import toolpackage.script_a and self.tools=[toolpackage.script_a.Tool1, etc...] –  Luke Jun 25 '13 at 21:45
1  
@Gnat Yes, Luke's thoughts are my experience as well. ESRI's publishing services don't recognize any of the from x import y style imports. (That is entirely ridiculous, by the way.) However, I believe you can leave your self.tools definition alone and use import toolpackage.script_a.Tool1 as Tool1. –  jpmc26 Feb 25 at 13:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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