20

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Alex Markov
  • 4,017
  • 22
  • 34
  • 3
    "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. – Remigijus Pankevičius Mar 07 '14 at 22:04

1 Answers1

18

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
import toolpackage.script_a.Tool1 as Tool1
import toolpackage.script_a.Tool2 as Tool2
import toolpackage.script_b.Tool3 as 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 = "mtt"

        # List of tool classes associated with this toolbox
        self.tools = [Tool1, Tool2, Tool3]
user2856
  • 65,736
  • 6
  • 115
  • 196
  • 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 06: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...] – user2856 Jun 25 '13 at 21:45
  • 4
    @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 '14 at 13:58
  • 1
    The alias should be short and only contain letters, i.e.
      self.alias = "mtt"
    
    – Curtis Price Nov 26 '15 at 09:00
  • @CurtisPrice Thanks for pointing the issue out. Back when I wrote this post I was still using 10.0 so couldn't test. – user2856 Nov 26 '15 at 22:50
  • 4
    Since I just spent an hour making changes to my tools code and trying to seem it reflected in the tool gui in Arcmap I would like to share this. If you make changes in your code outside of the pyt file and would like to see it reflected in the tool gui you have to restart ArcMap/Catalog. You can hit refresh on the toolbox all you want, that only seems to reload the pyt itself. It wont find the changes to the other code. – TurboGus Jun 16 '16 at 22:22
  • 1
    @TurboGus http://gis.stackexchange.com/q/91112/2856 – user2856 Jun 16 '16 at 22:49
  • I think this is the updated link to the ArcGIS forum: https://geonet.esri.com/thread/58731 – Richard Morgan Feb 22 '17 at 17:04
  • This did not work for me. First, I needed to add the toolpackage folder to the system path (sys.path.append(os.path.join(os.path.dirname(__file__), "Scripts"))), second, import only worked using from script_a import Tool1. With this method however, I'm still stuck with the reload issue, having to restart ArcGIS for changes in the py-Files to take effect. Any idea what I'm doing wrong? – Ratnanil Feb 06 '18 at 22:36
  • got it working.. with these inputs: https://stackoverflow.com/a/20749411/4139249 – Ratnanil Feb 14 '18 at 22:44