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

10-30-2019 12:24 PM
New Contributor III

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.



Tags (3)
0 Kudos
1 Reply
Esri Contributor


In the example they are 'hard-coding' the path to the default environment which is pretty safe to assume to exist. However, ArcGIS Pro can be configured to run using different CONDA environments as well, so the assumption that the default environment is used might not hold up. How about you write your custom code in a custom Python toolbox which is getting deployed along your add-in? Essentially I am suggesting having your python code in what appears to be a regular python tool to be executed via the GeoProcessing class and it would automagically work using the active Python environment. Things like the name or path location can be retrieved via Project.Current; in C# and passed as arguments into the geoprocessing tool.

The side benefit is that you are getting a GP tool for free which then even could executed outside your add-in just a regular Python tool in Pro by itself.

Calling C# functionality from Python is more of a generic question and maybe something like PyDotnet could be worth exploring. I haven't used the module myself but it certainly looks interesting.

Hope this helps,

- Thomas

0 Kudos