MakeTableView_management and MakeFeatureLayer_management won't work if arcpy.env.addOutputsToMap is set to False

2872
14
Jump to solution
08-02-2018 12:04 AM
simoxu
by MVP Regular Contributor
MVP Regular Contributor
arcpy.env.addOutputsToMap=False
arcpy.MakeTableView_management("<workspace>\\RDA.DBO.Immediate_Assessment",
                                       "tmp_tbl",where_clause)
# <Result 'tmp_tbl'>
arcpy.GetCount_management('tmp_tbl')
#   File "<string>", line 1, in <module>
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 17492, in GetCount
#     raise e
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 17489, in GetCount
#     retval = convertArcObjectToPythonObject(gp.GetCount_management(*gp_fixargs((in_rows,), True)))
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 496, in <lambda>
#     return lambda *args: val(*gp_fixargs(args, True))
# Error in sys.excepthook:
# Traceback (most recent call last):
#   File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\ptvsd\debugger.py", line 2409, in _excepthook
#     print_exception(exc_type, exc_value, exc_tb)
#   File "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Python\Core\ptvsd\debugger.py", line 2656, in print_exception
#     sys.stdout.write(out)
# AttributeError: 'NoneType' object has no attribute 'write'
# 
# Original exception was:
# Traceback (most recent call last):
#   File "<string>", line 1, in <module>
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 17492, in GetCount
#     raise e
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 17489, in GetCount
#     retval = convertArcObjectToPythonObject(gp.GetCount_management(*gp_fixargs((in_rows,), True)))
#   File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 496, in <lambda>
#     return lambda *args: val(*gp_fixargs(args, True))
# arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
# ERROR 000732: Input Rows: Dataset tmp_tbl does not exist or is not supported
# Failed to execute (GetCount).
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If the arcpy.env.addOutputsToMap is set to True, the same code will work.

arcpy.env.addOutputsToMap=True
arcpy.MakeTableView_management("<workspace>\\RDA.DBO.Immediate_Assessment",
                                       "tmp_tbl",where_clause)
arcpy.GetCount_management('tmp_tbl')
# <Result '45'>‍‍‍‍‍

What I really want to do is the same as the Python following example, but I don't want to add the temporary layer to the map (the code should not rely on an active map in my case)

# Name: DeleteFeatures_Example2.py
# Description: Delete features from a feature class based on an expression
 
# Import system modules
import arcpy
 
# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inFeatures = "parcels"
outFeatures = "C:/output/output.gdb/new_parcels"
tempLayer = "parcelsLayer"
expression = arcpy.AddFieldDelimiters(tempLayer, "PARCEL_ID") + " = 'Cemetery'"
 

# Execute CopyFeatures to make a new copy of the feature class
arcpy.CopyFeatures_management(inFeatures, outFeatures)
 
# Execute MakeFeatureLayer
arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)
 
# Execute SelectLayerByAttribute to determine which features to delete
arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION", 
                                        expression)
 
# Execute GetCount and if some features have been selected, then 
#  execute DeleteFeatures to remove the selected features.
if int(arcpy.GetCount_management(tempLayer)[0]) > 0:
    arcpy.DeleteFeatures_management(tempLayer)

the above code is from ESRI help document:

Delete Features—Data Management toolbox | ArcGIS Desktop 

So the question is:

Can we make in-memory feature layers or tables without adding them to a map? 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I logged a bug with Esri Support, you can attach your organization to it instead of submitting another one:

BUG-000116163 : In ArcGIS Pro when arcpy.env.AddOutputsToMap is set to False, the object fails to exist

View solution in original post

14 Replies
DanPatterson_Retired
MVP Emeritus

From here....

Considerations when using the in_memory workspace—Appendices | ArcGIS Desktop 

results are temporary

Data written to the in_memory workspace is temporary and will be lost when the application is closed, making in_memory ideal for intermediate data created from model and Python script tools.

and with Delete being available, why would you be concerned about results being added to the map?

simoxu
by MVP Regular Contributor
MVP Regular Contributor

Thanks Dan.

I am concerned that the temporary layer will mess up one of my maps. If I have an active map this will be my active map otherwise this will be first map in my map list (I guess...)

Although I can delete the records in the temp layer, but I assume the layer itself will still be listed in the content pane.

The point is, in my case I don't have a need to visualize this intermediate layer/table.

Hope this make sense.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you aren't adding the feature layer or table view to a map, you need to pass an object reference to other geoprocessing tools, not just the label:

arcpy.env.addOutputsToMap=False

res = arcpy.MakeTableView_management(
    "workspace>\\RDA.DBO.Immediate_Assessment",
    "tmp_tbl",
    where_clause
)
fl = res.getOutput(0)

arcpy.GetCount_management(fl)‍‍‍‍‍‍‍‍‍‍

The strings people commonly pass to geoprocessing tools are a kind of shorthand, a label, for an object.  ArcPy uses that label to search an index of sorts to get a reference to the object.  When addOutputsToMap is False the feature layer or table view isn't added to that index because it isn't part of any map, which is why you see the error that the dataset doesn't exist.  When addOutputsToMap is False  the object is created, you just need to capture a reference to the object from the result object returned from the geoprocessing tool.

simoxu
by MVP Regular Contributor
MVP Regular Contributor

Thanks Joshua.

I've tried that but no success.

Please have a look at the screenshot.

Python environment in ArcGIS Pro

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Although the Esri document link in the original question was for Pro, I wasn't sure you were working in Pro.  The code I posted works in ArcMap/ArcCatalog, which is where I have done something similar before.  I just tried it in Pro, and it does not work because the Result object from the geoprocessing tool isn't returning a reference to the object like in ArcMap.  I believe this should qualify as an equivalence bug if you open a case with Esri Support.

simoxu
by MVP Regular Contributor
MVP Regular Contributor

Good idea.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I logged a bug with Esri Support, you can attach your organization to it instead of submitting another one:

BUG-000116163 : In ArcGIS Pro when arcpy.env.AddOutputsToMap is set to False, the object fails to exist
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Thanks Joshua. I sent the bug report days ago to ESRI Tech Support (Local Distributor), they are still evaluating it.

Can you post the link of the bug please? I can't find it.

Cheers

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

The tech support has been informed about this bug, so that they don’t need to double handle it. Thanks

0 Kudos