Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 = ""

        # List of tool classes associated with this toolbox
        self.tools = [Capacity]


class Capacity(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
share|improve this question

2 Answers 2

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.

share|improve this answer
    
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 yesterday
    
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 13 hours ago
    
@user21053 They'll just be tool parameter values. – PolyGeo 7 hours ago

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)
share|improve this answer
    
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 22 hours ago
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. – Luke 22 hours ago
    
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 14 hours ago

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.