Select to view content in your preferred language

Setting scratchWorkspace and scratchGDB  in a Python Toolbox

1393
2
05-23-2013 10:51 AM
JohnDye
Deactivated User
I know that since Python Toolboxes are run in an application, they probably honor the executing application's environment settings. However, I also know if you explicitly establish an environment setting in a Python Script, such as env.scratchWorkspace, then the script is supposed to override the application's environment setting.

I need to set a scratchWorkspace and scratchGDB in a Python toolbox tool to ensure that intermediate data locations are consistent no matter which system is executing the tool, but it keeps throwing a syntax error, even though the syntax is correct. If I type the the statement into the Python Window:
import arcpy
from arcpy import env

env.

Then intellisense brings up the scratchWorkspace and scratchGDB functions just fine.

But if I do the same in a Python Toolbox, I get a syntax error.

Are workspaces not supported programmatically in a python toolbox? Do I really have to have my users go and set it through the GUI?

You know, I dont know much in this small, cruel world, but ESRI, that ain't right. That. Just. Ain't. Right.


Disclaimer: It is highly possible that workspaces are programmatically supported in python toolboxes and I am just a bit of a moron.
Tags (2)
0 Kudos
2 Replies
LucasDanzinger
Esri Frequent Contributor
Hmm, I just added arcpy.env.scratchWorkspace = r"C:\temp" to an existing PYT and I didn't get any syntax errors and it ran through successfully. Maybe you could post more code?
JohnDye
Deactivated User
Hmm...

I was previously establising the scratchWorkspace under the Execute defintion and I swear it was throwing syntax errors. Ok, chalk that one off to user error, I guess.
def execute(self, parameters, messages):
        """The tool logic to perform once the tool is executed."""
arcpy.env.workspace = "C:\Temp"


Works fine, except I  decided look at the Scratch Workspace in the tool GUI and the value is blank. Shouldn't the value "C:\Temp" be populated in the GUI?

In any case, I cant get scratchGDB to take without an error.
class Tool(object):
    def __init__(self):
        """Define the tool's attributes."""
        self.label = "MyTool"
        self.description = "Tool1"
        self.canRunInBackground = False
        arcpy.env.scratchGDB = r"C:\Temp\MyGDB.gdb"

When I do that, 'MyTool' throws an error:

Traceback (most recent call last):  File "<string>", line 31, in __init__  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 529, in set_    self[env] = val  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 581, in __setitem__    ret_ = setattr(self._gp, item, value)AttributeError: Object: Environment <scratchGDB> cannot be set


It doesn't matter if I use scratchGDB at _init_, getParameterInfo, or execute, result is always the same.
0 Kudos