Select to view content in your preferred language

python script tool (3 questions)

229
6
3 weeks ago
Seyed-MahdySadraddini
Regular Contributor

I have made my python script tool. I have several questions I am hoping to get answers for:

(Note: I am using ArcGIS Pro 3.1)

1. The tool generates the desired layer in the current geodatabase. It has to be added to the content pane manually for display. However, I want the layer to be added to the display automatically upon generation. I have added the below code to the tool but there is error when it gets to the last line:

# ArcGIS map interface
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.addLayer(layer_name)

 

ERROR:

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2560, in addLayer
return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
ValueError: test1_April23

test1_April23 layer generated and it exists in the current geodatabase! I don't understand why m.addLayer(layer_name) is causing an error! How else should I accomplish this task of automatically adding the generated layer to contents for display?

SeyedMahdySadraddini_0-1745411552318.png

 

2. When I share the python script tool with other people, I understand I can just zip the toolbox and the script file together and send it away. The issue is that the toolbox will look for the script at a designated directory. If that directory does not exist, the tool will have no associated code; unless the operator copies and pastes the code from the script file I sent them. Is there a way to avoid this requirement, so the operator does not have to manually open the script file to copy/paste the code?

 

3. Could the python script tool be shared with everybody through ArcGIS Enterprise?

0 Kudos
6 Replies
MichaelVolz
Esteemed Contributor

For #1 I think you need to use the explicit path to Pro project in a tool and not current.  I believe there are other past forum posts that have discussed this issue.

0 Kudos
Seyed-MahdySadraddini
Regular Contributor

I used the explicit path to the .aprx file. I am still getting the same ValueError as before. This tells me "CURRENT" can be substituted for the .aprx file as well; please correct me if that is not right!

As for the project path, I tested that as well (project folder path, not the .aprx file); this resulted in OSError.

0 Kudos
CodyPatterson
MVP Regular Contributor

Hey @Seyed-MahdySadraddini 

For number 1, I do something like this here:

output_feature_class = r'C:\Default.gdb\Filtered_Addresses'
map.addDataFromPath(output_feature_class)

This adds the feature layer to the map, but this may require an exclusive lock on the project, it has caused issues in the past with me, but may work for you, otherwise, try your original function with a variable that's set as the Absolute path where it originates from the C:/ drive or other drive you're using.

I'm a little confused about part 2, normally what happens is the tool and toolbox are zipped, and then it's sent out, unpacked, and the toolbox and tool can then be automatically added.

CodyPatterson_0-1745420619940.png

They can use "Add Toolbox" and add the toolbox that way, but I could be interpreting this incorrectly, please correct me if so!

For part 3, here is how to upload a custom python package, which should include your tool:

https://enterprise.arcgis.com/en/server/latest/publish-services/windows/deploying-custom-python-pack...

Cody

0 Kudos
Seyed-MahdySadraddini
Regular Contributor

See Below for the fix

0 Kudos
Seyed-MahdySadraddini
Regular Contributor

fixed it.

# Export data to new Feature Class
s2 = arcpy.conversion.ExportFeatures(s, layer_name)
arcpy.AddMessage(f'The name of the layer created in the default geodatabase is: {layer_name}')

# ArcGIS map interface
display_layer = arcpy.Describe(s2).catalogPath
arcpy.AddMessage(f'Path to the display layer is: {display_layer}')
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.addDataFromPath(display_layer)

Thanks @CodyPatterson for your guidance!

0 Kudos
CodyPatterson
MVP Regular Contributor

Hey @Seyed-MahdySadraddini 

Great job! Glad you got that checked out, hopefully that solves most of your problems!

Cody

0 Kudos