1

I try to create a folder and Geodatabase at the beginning of my code, so all results will be saved in it. My code is:

class btn_WF(object):
        """Implementation for Addins_addin.button (Button)"""
        # this button allows the user to determine where he/she wants to store all output files
        # The model generates a folder called (), and a File GeoDataBase called ()
        # The model makes the generated GeoDataBase is the current workspace
        def __init__(self):
            self.enabled = True
            self.checked = False
        def onClick(self):
            import pythonaddins, arcpy
            # Let the user select a folder
            Work_Location = pythonaddins.OpenDialog("Please select a folder to store the project and files in", False, "C:/ModelBuilder/AAA","Select a Folder")
            # create a new folder to save the project in
            Project_Folder = arcpy.CreateFolder_management(Work_Location, "Flash Flood Risk Assessment Projcet")
            # create a GeoDataBase to store files and outputs
            Output_File_GDB=arcpy.CreateFileGDB_management(Project_Folder, "FFRA_WRRI.gdb")

            # the problem is the next line
            # the "arcpy.env.workspace" doesnt accept to equal the variable "Output_File_GDB"
            arcpy.env.workspace = Output_File_GDB

            print (arcpy.env.workspace)

it gives me the follwoing error

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing\_base.py", line 537, in set_
    self[env] = val
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing\_base.py", line 597, in __setitem__
    ret_ = setattr(self._gp, item, value)
RuntimeError: Object: Error in accessing environment <workspace>

Could you help me

enter image description here

4
  • Have you tried print Work_Location after setting it to see if it has the value you think? Doing this with the Python window will let you see the output.
    – PolyGeo
    Oct 10, 2016 at 10:52
  • Here is a copy of all checks >>> print Work_Location C:\ModelBuilder\AAA\New Folder (8) >>> print Project_Folder C:\ModelBuilder\AAA\New Folder (8)\Flash Flood Risk Assessment Projcet >>> print Output_File_GDB C:\ModelBuilder\AAA\New Folder (8)\Flash Flood Risk Assessment Projcet\FFRA_WRRI.gdb Oct 10, 2016 at 10:55
  • >>> arcpy.env.workspace = Output_File_GDB Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing_base.py", line 537, in set_ self[env] = val File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing_base.py", line 597, in setitem ret_ = setattr(self._gp, item, value) RuntimeError: Object: Error in accessing environment <workspace> >>> Oct 10, 2016 at 10:56
  • not necessarily the reason, but I would avoid blank spaces and special characters in my paths when working with ArcGIS
    – radouxju
    Oct 10, 2016 at 14:03

1 Answer 1

5

I had this problem before.

There are two solutions:

  1. You need to set the result of the geoprocessing tool "CreateFileGDB" as a variable using arcpy.getOutput method.

  2. Another way to set the GDB variable is to use the OS module.

Try:

result = arcpy.CreateFileGDB_management(Project_Folder, "FFRA_WRRI.gdb")
Output_File_GDB = result.getOutput(0)
arcpy.env.workspace = Output_File_GDB

or try:

import OS
Output_File_GDB = os.path.join(Project_Folder, "FFRA_WRRI.gdb")
arcpy.env.workspace = Output_File_GDB
2
  • When I print out 'result' and 'Output_File_GDB' the printed text is identical, what is the difference to arcpy? Feb 22, 2017 at 15:30
  • 1
    As your setting the "result" variable as a geoprocessing tool, "result" does not then become the output. The variable is still the tool, rather than the output of the tool. By using the "result.getoutput", you are identifying the result of that tool and then setting a variable against that.
    – MacroZED
    Feb 23, 2017 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.