I am relatively new in Python.
I have a project that I need to adapt to work with ArcGIS Pro. This code runs in FME PythonCaller using Python 2 working with ArcMap. Now I need to migrate to Python 3 which works with ArcGIS Pro. I had to adapt only the part referring to library ArcPy. Here is a part of the code:
import arcpy
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
pass
def check_layer_file(self, lyrPath):
# Collect errors in list
self.error_messages = []
# Check whether the layer file is a group layer
aprx = arcpy.mp.ArcGISProject(r'C:\Path\To\Project\test.lyr')
map_lyr = aprx.listMaps()[0]
lyrs = aprx.listLayers(lyrPath)
glyr = lyrs[0]
if not glyr.isGroupLayer:
self.error_messages.append("Message.")
# Loop over all layers
for lyr in lyrs:
if not lyr.isGroupLayer:
# Check if all layers have a source
if lyr.isBroken: # Returns True if a layer's data source is broken.
self.error_messages.append(
str(lyr) + " has a broken source.")
# Check if layer is a feature layer
if lyr.isFeatureLayer:
self.error_messages.append(
str(lyr) + " is a Feature Layer.")
# Check for symbology type
if lyr.symbologyType == "OTHER":
self.error_messages.append(str(lyr) + " unsupported.")
return self.error_messages
Whenever I run it I get an error message: Python Exception: CURRENT.
Python Exception <OSError>: CURRENT
Traceback (most recent call last):
File "<string>", line 54, in input
File "<string>", line 15, in check_layer_file
File "D:\Apps\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 344, in __init__
self._arc_object = arcgisscripting._mapping.ArcGISProject(*gp_fixargs((aprx_path,), True))
OSError: CURRENT
I imagine the code assumes I am running it within ArcGIS Pro with a valid project open, which is not the case.
I have tried pointing directly to the path of the layer but I get the same error only now showing the filepath directory instead of "CURRENT" (Python Exception: filepath example).
Does anyone know what I am doing wrong?

arcpy.mp.ArcGISProjectsort of says it all, you have to call the function with the path to an ArcGIS Project file (*.aprx).lyrxis a layer file. Thearcpy.mp.ArcGISProjectcode uses a project file:.aprxYou're passing in the wrong thing.