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

2902
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
14 Replies
deleted-user-1_r2dgYuILKY
Occasional Contributor III

Is there any news on this bug? I have a script that copies thousands of features, performs analysis and then deletes the copies. When I run this script in the Python window in ArcGIS Pro, the features copy to the TOC (ex. feature_new), and then are deleted. However, each time this happens, another copy with the original feature name and a number (ex. feature_old26) is created in the TOC, and remains after the script moves on to the next feature. So I have thousands of these in the TOC. If I use arcpy.env.addOutputsToMap = False in the script, it fails. ERROR 000732: Input Features: Dataset feature_old26 does not exist or is not supported

0 Kudos
JacqueHamilton
New Contributor III

I am also getting this error. Has there been any resolution Yet?  I can run my script alone in IDEL but when I'm running it in Notebooks it want's to add my hundreds of layers to the map which I don't need.

0 Kudos
JacqueHamilton
New Contributor III

Any resolution to this problem in ArcPro?  I'm running Notebooks in ArcPro.  #BUG-000116163 : In ArcGIS Pro when arcpy.env.AddOutputsToMap is set to False, the object fails to exist

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Unfortunately, no:  BUG-000116163: In ArcGIS Pro, when arcpy.env.AddOutputsToMap is set..  The "In Product Plan" means they acknowledge a defect, but they won't commit to when it will be addressed.  Given the defect was logged 2 years ago, "In Product Plan" is about as reassuring as "the check is in the mail."

0 Kudos
JacqueHamilton
New Contributor III

Thanks for the update and the link to the notification. I added myself to the notify list when it gets fixed. I guess I won't be using Notebooks in ArcPro any time soon. Not until they fix this. I hate having to manually delete the hundreds of temp layer files and shapefiles that get added the map when I'm running my model.

0 Kudos