AnsweredAssumed Answered

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

Question asked by simo on Aug 2, 2018
Latest reply on Aug 17, 2018 by simo
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? 

Outcomes