Select to view content in your preferred language

RuntimeError: cannot open workspace

12275
10
Jump to solution
04-04-2016 09:38 AM
ErnestoCarreras3
Regular Contributor

Purpose: Edit a versioned feature class within a Feature dataset in order to calculate a field

I have a field called Date to store the date of the inspection (data type: date)

I have another field called Time_Diff (data type: Long). This field is the one that will be used to calculate the time difference between system date and the date recorded in the Date field.

The script keeps failing due to the following runtime error: RuntimeError: cannot open workspace

Any assistance will be greatly appreciated.

# Import arcpy module
import arcpy


# Local variables:
Input_MapChangeRequest = "Database Connections\\GISHW.sde\\gishw.PU.HW_FieldCollection\\gishw.PU.MapChangeRequest"
MapChangeRequest_Layer = "MapChangeRequest_Layer"
MapChangeRequest_layer2 = MapChangeRequest_Layer
MapChangeRequest_Layer__3_ = MapChangeRequest_layer2

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Input_MapChangeRequest, MapChangeRequest_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;UTILITY UTILITY VISIBLE NONE;ISSUE ISSUE VISIBLE NONE;ASSET ASSET VISIBLE NONE;CRITICAL CRITICAL VISIBLE NONE;NOTES NOTES VISIBLE NONE;STATUS STATUS VISIBLE NONE;REPORTER REPORTER VISIBLE NONE;DATE DATE VISIBLE NONE;SHAPE SHAPE VISIBLE NONE;created_user created_user VISIBLE NONE;created_date created_date VISIBLE NONE;last_edited_user last_edited_user VISIBLE NONE;last_edited_date last_edited_date VISIBLE NONE;GlobalID GlobalID VISIBLE NONE;Time_Diff Time_Diff VISIBLE NONE")

# Process: Change Version
arcpy.ChangeVersion_management(MapChangeRequest_Layer, "TRANSACTIONAL", "ARCGIS_SERVER.PU_web_editing", "")

# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(MapChangeRequest_Layer)

# Edit session is started without an undo/redo stack for versioned data
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

# Process: Calculate Field
arcpy.CalculateField_management(MapChangeRequest_layer2, "Time_Diff", "(datetime.datetime.now() - arcpy.time.ParseDateTimeString( !DATE! )).days", "PYTHON_9.3", "")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Honored Contributor

What happens if you supply the workspace instead of the name of the layer from the workspace?

edit = arcpy.da.Editor(conn) or edit = arcpy.da.Editor(arcpy.env.workspace)

View solution in original post

10 Replies
DanPatterson_Retired
MVP Emeritus

are you missing something in your path? the location of the workspace needs to be explicit in the script from my reading of the help file

Editor—Help | ArcGIS for Desktop

maybe this env—Help | ArcGIS for Desktop needs to be added

0 Kudos
ErnestoCarreras3
Regular Contributor

I just added the following below Import arcpy and got the same error:

arcpy.env.workspace = "Database Connections\\GISHW.sde\\gishw.PU.HW_FieldCollection"

0 Kudos
DanPatterson_Retired
MVP Emeritus

I work locally, I suspect pure python and arcpy knows nothing about your connection

Create Database Connection—Help | ArcGIS for Desktop

so if it has worked in the past, I have no clue... and will defer to those that have to work on networks

ErnestoCarreras3
Regular Contributor

Anyways, thanks for you input.

0 Kudos
ErnestoCarreras3
Regular Contributor

I have modified the script so that it point to the SDE connection locally for the workspace but still I get the same runtime error... any help will he really appreciated . The SDE connection used in the script points to the version where I need to update the data.

# Import arcpy module
import arcpy

conn = r"U:\GIS\GISHW.sde"
arcpy.env.workspace = conn

# Local variables:
Input_MapChangeRequest = r"U:\GIS\GISHW.sde\gishw.PU.HW_FieldCollection\gishw.PU.MapChangeRequest"
MapChangeRequest_Layer = "MapChangeRequest_Layer"

# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(MapChangeRequest_Layer)

# Edit session is started without an undo/redo stack for versioned data
edit.startEditing(True)

# Start an edit operation
edit.startOperation()

# Process: Calculate Field
arcpy.CalculateField_management(MapChangeRequest_Layer, "Time_Diff", "(datetime.datetime.now() - arcpy.time.ParseDateTimeString( !DATE! )).days", "PYTHON_9.3", "")

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

Jake Skinner​ I noticed you commented on a similar question and thought you may be able to assist me with this issue.

Thanks in advance

0 Kudos
FreddieGibson
Honored Contributor

What happens if you supply the workspace instead of the name of the layer from the workspace?

edit = arcpy.da.Editor(conn) or edit = arcpy.da.Editor(arcpy.env.workspace)

ErnestoCarreras3
Regular Contributor

It returns the following error even though I have enabled the editing within the script...

arcgisscripting.ExecuteError: ERROR 999999: Error executing function.

Objects in this class cannot be updated outside an edit session [gishw.PU.MapChangeRequest]

Failed to execute (CalculateField).

0 Kudos
FreddieGibson
Honored Contributor

Is the table you're needing to edit registered as versioned?

0 Kudos
ErnestoCarreras3
Regular Contributor

Yes

Well, it appears it was not. I assumed it got unversioned after adding a new feature class to the Feature Dataset. Is that correct?

Now, the script works as expected.

Thanks a lot!

0 Kudos