0

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?

4
  • 1
    If you're not running with a project open, then you can't use CURRENT. Using a file path is likely lack of use of raw formatting (different issue)., but you need to include your code to be sure. Please Edit the Question.
    – Vince
    Dec 1, 2023 at 13:12
  • 1
    The name of arcpy.mp.ArcGISProject sort of says it all, you have to call the function with the path to an ArcGIS Project file (*.aprx).
    – bixb0012
    Dec 1, 2023 at 14:47
  • Ha, so it needs to be a .lyrx file. A .lyr is not going to work. Did I understand correctly?
    – Bram
    Dec 1, 2023 at 15:24
  • 1
    No - a lyrx is a layer file. The arcpy.mp.ArcGISProject code uses a project file: .aprx You're passing in the wrong thing.
    – KHibma
    Dec 1, 2023 at 17:33

1 Answer 1

1

first change this line - the project file extension is .aprx

        aprx = arcpy.mp.ArcGISProject(r'C:\Path\To\Projectaprx\test.aprx')

example:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.