3

I am learning Python Toolboxes with PyScripter.

I have a test python toolbox that outputs a raster file. It works when run from ArcCatalog.

As I understand it, it should also run standalone within Pyscripter but when I run it, no output appears in the Python Interpreter, and no file is created in the target geodatabase. I also can't locate advice on how to make the messages appear within the Pyscripter window, as they do in ArcCatalog, eg to indicate the analysis has finished within PyScripter without having to check in ArcCatalog.

I tried from the "Run" command on the drop down menu and from the "external run"

I am using ArcGIS Desktop 10.2.2

import arcpy
import os
import sys
from arcpy.sa import *

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

        # List of tool classes associated with this toolbox
        self.tools = [CapacityTool, DemandTool]


class CapacityTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Capacity"
        self.description = "Calculates capacity score "
        self.canRunInBackground = False

    def getParameterInfo(self):

        p0 = arcpy.Parameter(
            displayName="Cell size and extent",
            name="SAcell",
            datatype="DERasterDataset",
            parameterType="Required",
            category = "Default cell size and extent" ,
            direction="Input")
        p0.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES1BaseMap\Outputs.gdb', 'SA010')

        p1 = arcpy.Parameter(
            displayName="Outputs",
            name="Outputs",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p1.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES2Carbon_storage_regulation\Outputs.gdb')

        p2 = arcpy.Parameter(
            displayName="Scratch",
            name="Scratch",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p2.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES2Carbon_storage_regulation\Scratch.gdb')

        # parameter list

        return [p0, p1, p2]


    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return


    def execute(self, parameters, messages):
        """The source code of the tool."""

        SAcell             = parameters[0].valueAsText
        Outputs            = parameters[1].valueAsText
        Scratch            = parameters[2].valueAsText


        # set analysis environment e.g. extent, cell size, snap raster

        ext2                 = arcpy.Describe(SAcell).extent
        arcpy.env.extent     = ext2
        cell1                = parameters[0].valueAsText
        arcpy.env.cellSize   = cell1
        arcpy.env.snapRaster = parameters[0].valueAsText


        # set all local variables used in this model analysis

        test                 = "test"
        out2               = os.path.join(Outputs, test)


        # set overwrite and scratch workspace

        arcpy.env.overwriteOutput = True
        arcpy.env.workspace = Scratch
        arcpy.CheckOutExtension('Spatial')

        messages.addMessage("Starting analysis")


        arcpy.CopyRaster_management(cell1, out2)

        del test, cell1

        messages.addMessage("Analysis finished")

        return

class DemandTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Demand"
        self.description = "Calculates demand score "
        self.canRunInBackground = False
6

Python Toolboxes rely upon the Geoprocessing Framework provided by the applications of ArcGIS for Desktop to generate their tool dialogs.

I am not aware of any documentation that suggests that they will work standalone in a Python IDE such as PyScripter, unless you are importing them into another Python script using a syntax like:

arcpy.ImportToolbox(r"C:\Users\me\my.pyt") arcpy.mynewtools.MyTool("a", "b", "c")

The example code above comes from a GeoNet thread that may be worth reviewing for additional information.

  • thanks, i'll check that. i has assumed if all the tool dialogs had default parameter values that it should be able to run in the same way that python scripts can run outside ArcGIS. – user21053 Nov 29 '15 at 21:47
  • Thanks, yes that looked interesting but I was not able to fully follow it for my example, e.g. what the ("a", "b", "c") relates to. – user21053 Nov 30 '15 at 16:15
  • @user21053 They'll just be tool parameter values. – PolyGeo Nov 30 '15 at 23:03
7

When you click "Run" or call python mytoolbox.pyt, your code does not do anything other than define the classes and import some modules. You haven't actually told python to run the tool.

The best way to run your tool is using arcpy.ImportToolbox from another script or from the python prompt as per PolyGeo's answer.

However, what I sometimes do for debugging purposes so I can step through the code is instantiate the tool and call the execute method directly, something like the following. Note this won't always work and you need to do a bit of kludging...

class Capacity(object):
    def __init__(self):
        etc...

    def getParameterInfo(self):

        p0 = arcpy.Parameter(
            displayName="Cell size and extent",
            name="SAcell",
            datatype="DERasterDataset",
            parameterType="Required",
            category = "Default cell size and extent" ,
            direction="Input")
        p0.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES1BaseMap\Outputs.gdb', 'SA010')

        p1 = arcpy.Parameter(
            displayName="Outputs",
            name="Outputs",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p1.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES2Carbon_storage_regulation\Outputs.gdb')

        p2 = arcpy.Parameter(
            displayName="Scratch",
            name="Scratch",
            datatype="DEWorkspace",
            parameterType="Required",
            category = "Default data location",
            direction="Input")
        p2.value =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'ModelOutputs\ES2Carbon_storage_regulation\Scratch.gdb')

        # parameter list

        return [p0, p1, p2]


    etc...

    def execute(self, parameters, messages):
        """The source code of the tool."""

        SAcell             = parameters[0].valueAsText
        Outputs            = parameters[1].valueAsText
        Scratch            = parameters[2].valueAsText

        etc...

if __name__ == '__main__': #Don't leave this code block in when you've finished debugging, ArcGIS doesn't like it...
    class messages(object): #Kludge to fake the `messages` object
        addMessage = staticmethod(arcpy.AddMessage)
        addErrorMessage = staticmethod(arcpy.AddError)
        addWarningMessage = staticmethod(arcpy.AddWarning)
        addGPMessages = staticmethod(arcpy.GetMessages)

    tool = Capacity()
    params = tool.getParameterInfo()
    for p in params: #Kludge as these parameter objects don't have `valueAsText` properties
        p.valueAsText = str(p.value)
    tool.execute(params, messages)
  • An additional option for running your tool is to segregate your code from the toolbox (that is, make the toolbox do nothing but call an imported function) and create a separate runner script to call it with arguments. This has a couple of benefits, imo. The biggest is that it forces you to segregate your code logic from the logic of setting up and obtaining parameters. – jpmc26 Nov 30 '15 at 7:17
  • 1
    Good point, and that's my usual way of doing things as the logic can be imported into other scripts or turned into a command line utility if desired. – user2856 Nov 30 '15 at 7:20
  • Thanks for these. I have not been able to get these to work from these examples, and I don't want to deviate too far from the toolbox templates i am using. I will stick with testing the toolboxes in ArcCatalog. I had just hoped that since all the toolboxes I am working with will always have default data, and so be ready to run, that they might work from outside ArcGIS. I also found this link below which indicates another way to debug toolboxes in an IDE. blogs.esri.com/esri/arcgis/2012/12/14/… – user21053 Nov 30 '15 at 16:01
0

I am adding an additional example here to illustrate a few issues with importing a python toolbox. The context of my use was to rapidly test and run a sequence of python toolboxes in situations where there is no need to interact with the toolbox parameters. All the toolboxes have default parameters set. This can be useful when testing toolboxes or as a rapid option to run through a sequence of tools in "default mode" but also allow users to independently open and run each tool by altering the tool parameters where needed. I found it especially useful where one tool create outputs that can then be used as input for the next tool, allowing the tools to quickly be run in sequence.

I decided to use this method so I could leave the script in each python toolbox intact, rather than having to alter the script for testing and then edit after. There is discussion in the other answers here and associated links about alternative methods to execute the imported tools.

There are two options to import and then run python toolboxes to fit this situation.

  1. create a simple python script (.py) to import and run the tools within the python toolboxes.
  2. create a python toolbox (.pyt) with simple boolean parameters to import and run each python toolbox.

The choice would depend on the expected users, e.g. to stay within ArcGIS or to allow use outside ArcGIS.The issues I found were:

For a toolbox that is named carbon_storage.pyt, with toolbox alias of "CarbonToolbox", and where tools are named "CapacityTool" and "DemandTool" then

  1. Method within a simple python script (.py) to import and run the two tools in sequence. This allow the toolboxes to easily be run from an IDE e.g. PyScripter.

This is the format recommend on the ESRI website and this ran ok for me.

arcpy.ImportToolbox ("<path>\carbon_storage.pyt"    
arcpy.CapacityTool_CarbonToolbox()    
arcpy.DemandTool_CarbonToolbox()

The alternative method also worked.

arcpy.ImportToolbox ("<path>\carbon_storage.pyt")    
arcpy.CarbonToolbox.CapacityTool()   
arcpy.CarbonToolbox.DemandTool()
  1. Method using a separate toolbox to control tools within imported toolboxes (.pyt)

I found I had to repeatedly import the toolbox immediately before each execute

arcpy.ImportToolbox ("<path>\carbon_storage.pyt")    
arcpy.CapacityTool_CarbonToolbox()    
arcpy.ImportToolbox ("<path>\carbon_storage.pyt")    
arcpy.DemandTool_CarbonToolbox()

The alternative method also worked.

arcpy.ImportToolbox ("<path>\carbon_storage.pyt") 
arcpy.CarbonToolbox.CapacityTool()  
arcpy.ImportToolbox ("<path>\carbon_storage.pyt")
arcpy.CarbonToolbox.DemandTool()

Because some of the python toolbox tools I was running take a very long time I'd expected there to be some time difference between the two tool execution methods or the use from the IDE or via ArcGIS, and also perhaps a difference if a copy of ArcGIS was open (but inactive) vs. no copies open whilst running the script from PyScripter.

From tests it appears that the alternative method (toolboxalias.toolname) is quicker than the ESRI method (toolname_toolboxalias). This was consistent when testing both within .py scripts and the .pyt toolbox. When testing with python scripts the script seemed to complete quicker when no ArcGIS software was open (even if not in use).

Perhaps surprisingly the quickest method was when run from the python toolbox.

The rank order was:

  1. Run from python toolbox. Tool execute, ESRI format (time = 100%)
  2. Run from python script (no ArcGIS software open). Tool execute, ESRI format.(88.8% of max time)
  3. Run from python script (no ArcGIS software open). Tool execute, alternative format. (84% of max time)
  4. Run from python toolbox. Tool execute, alternative format. (81% of max time)

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.