I'm quite late, but I haven't been able to get this method to work. The syntax on the .pyt is OK (i.e.: I'm able to view the toolbox within arcmap without it displaying the red X), but when I click the drop-down for the toolbox, it displays no tools.
My directory is set up as such:
Tools/
- bhaTools.pyt
- publishing.py
-findingpath.pyc
The findingpath.pyc:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">findpath</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> local_path <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>abspath<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>__file__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> local_path <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The bhaTools.pyt has the following:
<SPAN class="keyword token">import</SPAN> contextlib <SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> sys <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">import</SPAN> glob <SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">Toolbox</SPAN><SPAN class="punctuation token">(</SPAN>object<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> self<SPAN class="punctuation token">.</SPAN>label <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'bhaTools'</SPAN> self<SPAN class="punctuation token">.</SPAN>alias <SPAN class="operator token">=</SPAN> <SPAN class="string token">'bhaTools'</SPAN> <SPAN class="keyword token">import</SPAN> findingpath path <SPAN class="operator token">=</SPAN> findingpath<SPAN class="punctuation token">.</SPAN>findpath<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> self<SPAN class="punctuation token">.</SPAN>tools<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> py <SPAN class="keyword token">in</SPAN> glob<SPAN class="punctuation token">.</SPAN>glob<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'*.py'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> __file__ <SPAN class="keyword token">in</SPAN> py<SPAN class="punctuation token">:</SPAN> module<SPAN class="operator token">=</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>py<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#Note: "Tool" class must have same</SPAN> <SPAN class="comment token">#name as the script "Tool" classes</SPAN> self<SPAN class="punctuation token">.</SPAN>tools<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>__import__<SPAN class="punctuation token">(</SPAN>module<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>Tool<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The publishing.py:
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">Tool</SPAN><SPAN class="punctuation token">(</SPAN>object<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> self<SPAN class="punctuation token">.</SPAN>label <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'Publish Parcel Fabric Parcels'</SPAN> self<SPAN class="punctuation token">.</SPAN>canRunInBackground <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN> self<SPAN class="punctuation token">.</SPAN>category <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Parcel_Fabric_Migration_Tools"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getParameterInfo</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> param_1 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Parameter<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> param_1<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'inputParcelFabricParcels'</SPAN> param_1<SPAN class="punctuation token">.</SPAN>displayName <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'Input Parcel Fabric Parcels'</SPAN> param_1<SPAN class="punctuation token">.</SPAN>parameterType <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Required'</SPAN> param_1<SPAN class="punctuation token">.</SPAN>direction <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Input'</SPAN> param_1<SPAN class="punctuation token">.</SPAN>datatype <SPAN class="operator token">=</SPAN> u<SPAN class="string token">'DEFeatureClass'</SPAN> <SPAN class="comment token">#### .... more parameters .... #### </SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">[</SPAN>param_1<SPAN class="punctuation token">,</SPAN>param_2<SPAN class="punctuation token">,</SPAN>param_3<SPAN class="punctuation token">,</SPAN>param_4<SPAN class="punctuation token">,</SPAN>param_5<SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">isLicensed</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateParameters</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> validator <SPAN class="operator token">=</SPAN> getattr<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ToolValidator'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> validator<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> validator<SPAN class="punctuation token">(</SPAN>parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>updateParameters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateMessages</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> validator <SPAN class="operator token">=</SPAN> getattr<SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ToolValidator'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> validator<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> validator<SPAN class="punctuation token">(</SPAN>parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>updateMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># tool/parameter setup methods etc...</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">execute</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">,</SPAN><SPAN class="operator token">**</SPAN>kwargs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">,</SPAN><SPAN class="operator token">**</SPAN>kwargs<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">,</SPAN><SPAN class="operator token">**</SPAN>kwargs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># do stuff with params</SPAN> <SPAN class="keyword token">except</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ExecuteError<SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> __name__<SPAN class="operator token">==</SPAN><SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token">#Run script</SPAN> main<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This is somewhat similar to how the Spatial Analyst Supplemental tools area set up. This structure has the advantage of the .py files could be run either from the .pyt or added as pyx script tools.
I am finally getting into this and really appreciate the discussion and Luke's examples.
Another tip on tbx's that is helping me is to run the Jason Scheirer's tbx2pyt tool, and then stealing the results to copy and paste into the getParameterInfo() code, which seems to be the most time-consuming part of this.
One thing about this that is kind of unnerving is there really isn't a very standard way to do it (yet). That will take time I guess, as people do different things for example
Python toolboxes provide the opportunity to take advantage of your Python skills and create tools entirely and easily out of Python
At ArcGIS 9.0, the script tool framework was first introduced and is geared towards creating Python-based tools for new users, but it is an inefficient process for more experienced users. In the script tool framework:You define parameters through the wizardYou create validation code that lives in the toolboxPlus you create and maintain the source script separatelyAll of these parts are segregated and more difficult to manage collectively.
if i could find a way to just publish the pyt once then throw the extra scripts in there and have them recognized that would be the bomb
# \--SomeDir # | toolbox.pyt # | some_script.py # | another_script.py # | ... # | last_script.py # # Each *.py contains a class called Tool that's just a stub for your "main" def. #---------------------------- #The .pyt file (all Tools are dynamically imported not hardcoded) #---------------------------- import arcpy import os,sys,glob 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 Dynamically Imported Tools" # Dynamic list of tool classes associated with this toolbox path=os.path.dirname(__file__) self.tools=[] for py in glob.glob(os.path.join(path,'*.py')): if not __file__ in py: module=os.path.basename(py)[:-3] #Note: "Tool" class must have same #name as the script "Tool" classes self.tools.append(__import__(module).Tool) #---------------------------- #The .py script files #---------------------------- #"Tool" class can be called anything but must be # the same in each script and in the calling toolbox class Tool(object): # tool/parameter setup methods etc... def execute(self,*args,**kwargs): main(*args,**kwargs) def main(*args,**kwargs): #do stuff return if __name__=='__main__': #Run script main(sys.argv[1:])
#---------------- # 10.0 script tool # script_10_0.py #---------------- import arcpy def do_some_stuff(arg1,arg2): print arg1,arg2 if __name__ == '__main__': arg1=arcpy.GetParameterAsText(0) arg2=arcpy.GetParameterAsText(1) do_some_stuff(arg1,arg2) #---------------------- # 10.1 Python Toolbox #---------------------- import arcpy import script_10_0 #import the old 10.0 script tool so you can call its functions. class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "10_0_Tool_Toolbox" self.alias = "Toolbox with 10.0 Tools" # List of tool classes associated with this toolbox self.tools = [Tool1] class Tool1(object): #... def execute(self, parameters, messages): arg1=parameters[0].valueAsText arg2=parameters[1].valueAsText script_10_0.do_some_stuff(arg1,arg2)
I'm still using 10.0 so can't actually test this, but have you tried using the standard python package structure?
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 = "mtt" # List of tool classes associated with this toolbox self.tools = [Tool1, Tool2, Tool3]
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.