Select to view content in your preferred language

arcpy.management.DeleteField working in pro but not online.

776
3
Jump to solution
05-15-2023 04:33 PM
Labels (3)
MattTarkington1
New Contributor III

Hi everyone,

I'm trying to take some models in ArcPro that I've built, and translate them into a AGOL Advanced notebook. (It'd be a lot easier to just leave this in models, but IT issues cause periodic disconnect from portal mid processes and it corrupts the data) Originally I was building the notebook in pro for testing. However I just tried it in the online environment and am not having any success. 

Here's the code (works in an ArcPro Notebook.)

from arcgis.gis import GIS
gis = GIS("home")
import arcpy
line_url = ("https://services8.arcgis.com/kiN57DOJrIhpvBby/arcgis/rest/services/<LineLayer>/FeatureServer/98")
table_url = ("https://services8.arcgis.com/kiN57DOJrIhpvBby/arcgis/rest/services/<TableForJoin>/FeatureServer/0")
arcpy.management.DeleteField(in_table=line_url, drop_field=["Freight", "Price_Per_Foot", "Price_Per_Unit", "Type", "Description" ])[0]

arcpy.management.JoinField(
    in_data=line_url,
    in_field="Lock",
    join_table=table_url,
    join_field="Key_",
    fields="Description;Price_Per_Foot;Price_Per_Unit;Freight",
    fm_option="NOT_USE_FM",
    field_mapping=None
)

 

And the error (returned in AGOL Notebook, no errors in pro):

ExecuteError                              Traceback (most recent call last)
Input In [110], in <cell line: 2>()
      1 #Drop Fields
----> 2 arcpy.management.DeleteField(
      3     in_table=line_url,
      4     drop_field="Lock;Description;Price_Per_Foot;Price_Per_Unit;Freight",
      5     method="DELETE_FIELDS"
      6 )
      8 arcpy.management.JoinField(
      9     in_data=line_url,
     10     in_field="Lock",
   (...)
     15     field_mapping=None
     16 )

File /opt/conda/lib/python3.9/site-packages/arcpy/management.py:6025, in DeleteField(in_table, drop_field, method)
   6023     return retval
   6024 except Exception as e:
-> 6025     raise e

File /opt/conda/lib/python3.9/site-packages/arcpy/management.py:6022, in DeleteField(in_table, drop_field, method)
   6020 from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject
   6021 try:
-> 6022     retval = convertArcObjectToPythonObject(gp.DeleteField_management(*gp_fixargs((in_table, drop_field, method), True)))
   6023     return retval
   6024 except Exception as e:

File /opt/conda/lib/python3.9/site-packages/arcpy/geoprocessing/_base.py:512, in Geoprocessor.__getattr__.<locals>.<lambda>(*args)
    510 val = getattr(self._gp, attr)
    511 if callable(val):
--> 512     return lambda *args: val(*gp_fixargs(args, True))
    513 else:
    514     return convertArcObjectToPythonObject(val)

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset https://services8.arcgis.com/kiN57DOJrIhpvBby/arcgis/rest/services/<LineLayer>/FeatureServer/98 does not exist or is not supported
Failed to execute (DeleteField).

So my end goal here is to have a table hosted in AGOL that when updated, will update prices in hosted Feature Layers. Then on to field calculates to get price of line based on (feet*price per foot). 

I'm deleting fields before joining because if I don't delete, the join just continually adds more sequential fields that do not fit in with the field calculation done later in the script (at least that's how it worked in model builder). 

I've also tried gis.content.get, and converting to the FeatureLayer module in this script, and get a different error:

RuntimeError                              Traceback (most recent call last)
Input In [111], in <cell line: 2>()
      1 #arcpy.management.DeleteField(in_table=lineFeature, drop_field=["Freight", "Price_Per_Foot", "Price_Per_Unit", "Type", "Description"])[0]
----> 2 arcpy.management.DeleteField(
      3     in_table=FuturePipe,
      4     drop_field="Lock;Description;Price_Per_Foot;Price_Per_Unit;Freight",
      5     method="DELETE_FIELDS"
      6 )
      8 arcpy.management.JoinField(
      9     in_data=lineFeature,
     10     in_field="Lock",
   (...)
     15     field_mapping=None
     16 )

File /opt/conda/lib/python3.9/site-packages/arcpy/management.py:6025, in DeleteField(in_table, drop_field, method)
   6023     return retval
   6024 except Exception as e:
-> 6025     raise e

File /opt/conda/lib/python3.9/site-packages/arcpy/management.py:6022, in DeleteField(in_table, drop_field, method)
   6020 from arcpy.arcobjects.arcobjectconversion import convertArcObjectToPythonObject
   6021 try:
-> 6022     retval = convertArcObjectToPythonObject(gp.DeleteField_management(*gp_fixargs((in_table, drop_field, method), True)))
   6023     return retval
   6024 except Exception as e:

File /opt/conda/lib/python3.9/site-packages/arcpy/geoprocessing/_base.py:512, in Geoprocessor.__getattr__.<locals>.<lambda>(*args)
    510 val = getattr(self._gp, attr)
    511 if callable(val):
--> 512     return lambda *args: val(*gp_fixargs(args, True))
    513 else:
    514     return convertArcObjectToPythonObject(val)

RuntimeError: Object: Error in executing tool

If anyone knows of a better way to do this or how to get the delete then join function working correctly, I very much appreciate the help.

Cheers,

Matt

0 Kudos
1 Solution

Accepted Solutions
MattTarkington1
New Contributor III

Just in case anyone else is looking for a similar solution, After working with tech support we discovered that in the AGOL notebook, you had to use arcpy.SignInToPortal("https://arcgis.com", "<username>", "<password>") to get access to arcpy modules and functions. Using my organizations portal address did not work, but arcgis.com did.

View solution in original post

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Not sure about the arcpy tool approach, but there seems to be a number of restrictions doing it manually which probably apply

Add or delete a field—ArcGIS Online Help | Documentation


... sort of retired...
0 Kudos
MattTarkington1
New Contributor III

Thank you for the response. However I'm using a test layer and none of the restrictions apply to my layer. And the script is working fine in ArcPro. I think it's something to do with setting the environment, but in Pro I'm just pulling the layer directly from AGOL and haven't set the environment either.

0 Kudos
MattTarkington1
New Contributor III

Just in case anyone else is looking for a similar solution, After working with tech support we discovered that in the AGOL notebook, you had to use arcpy.SignInToPortal("https://arcgis.com", "<username>", "<password>") to get access to arcpy modules and functions. Using my organizations portal address did not work, but arcgis.com did.

0 Kudos