如何抑制Python AttributeError?

问题描述

当我运行ArcGIS Pro Add Geometry Attributes (Data Management)命令时:

arcpy.AddGeometryAttributes_management()

我收到以下错误

Traceback (most recent call last):

  File "<string>",line 1,in <module>

AttributeError: 'ToolValidator' object has no attribute 'isLicensed'

该工具执行所需的操作并正常工作。但是,进行一些研究表明,这是known bug,应该忽略:

是的,因为该工具没有许可证要求 (https://pro.arcgis.com/en/pro-app/tool-reference/conversion/table-to-excel.htm#L_) 只是忽略并继续前进。我对GPS团队进行了ping操作,以确保他们 可以看看这个最近报告的问题。我很抱歉你 碰上它,但看起来并不重要...

有什么办法可以完全抑制这样的AttributeError,这样在较大的工作流程中看不到错误

解决方法

只需在要忽略错误的代码行周围运行try-except块即可。

try:
    arcpy.AddGeometryAttributes_management()
except AttributeError:
    pass

这将忽略该错误并继续。

鉴于似乎错误可能在模块中,您可以只使用except:,但这将忽略返回的每个错误。