AnsweredAssumed Answered

Calling Python scripts from C# Add-In code (and vice versa)

Question asked by david.lamartina_NCS on Oct 30, 2019
Latest reply on Oct 30, 2019 by temge-esristaff

We're writing an Add-In that will most likely need to call a Python script to perform certain actions on a .aprx Project file. I've seen at least one example for doing that - Run a geoprocessing task and python script | ArcGIS for Developers. In that example, there's a hard-coded path to the Python executable installed with ArcGIS Pro. However, I'm not sure if it's safe to assume every user will have that executable installed to the standard path specified in the ArcGIS Pro installer.

 

Is there a better (and more sure-fire) way to call a Python script from C# code, specifically in the context of an Add-In? Some ArcGIS SDK method for getting at the actual Python session taking place in an ArcGIS Pro session? It'd be great to be able to do this more elegantly, and to get at the return values of the script without creating a pipe or additional output file, as well.

 

I would think so, given the information given here: ArcGISProject—ArcPy | ArcGIS Desktop . Specifically, with regards to getting an ArcGISProject object:

 

The second technique is to use the CURRENT keyword as an input parameter to the ArcGISProject function. This technique only works from within an ArcGIS Pro application because the ArcGISProject object references the project that is currently loaded into the ArcGIS Pro application. The CURRENT keyword can be used in the Python window or with geoprocessing script tools.

 

We're not going to be in the Python window, since this is a script being run from a C# application. How would we use geoprocessing script tools to make this happen?

 

Similarly, we'll probably want to call some C# dlls from our Python code, rather than writing everything we need in that script in Python. Best practices for this? That might be more a general C# / .NET question than an ESRI question.

 

 

Thanks,

 

 

 

David

Outcomes