<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calling Python scripts from C# Add-In code (and vice versa) in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/calling-python-scripts-from-c-add-in-code-and-vice/m-p/822531#M2893</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;David,&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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 &lt;A href="https://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/creating-a-new-python-toolbox.htm"&gt;Python toolbox&lt;/A&gt; 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 &lt;SPAN style="font-family: 'andale mono', monospace;"&gt;GeoProcessing&lt;/SPAN&gt; class and it would automagically work using the active Python environment. Things like the name or path location can be retrieved via &lt;SPAN style="font-family: 'andale mono', monospace;"&gt;Project.Current;&lt;/SPAN&gt; in C# and passed as arguments into the geoprocessing tool.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;Calling C# functionality from Python is more of a generic question and maybe something like &lt;A href="https://bitbucket.org/pydotnet/pydotnet/wiki/Home"&gt;PyDotnet &lt;/A&gt;could be worth exploring. I haven't used the module myself but it certainly looks interesting.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;- Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 30 Oct 2019 21:37:44 GMT</pubDate>
    <dc:creator>ThomasEmge</dc:creator>
    <dc:date>2019-10-30T21:37:44Z</dc:date>
    <item>
      <title>Calling Python scripts from C# Add-In code (and vice versa)</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/calling-python-scripts-from-c-add-in-code-and-vice/m-p/822530#M2892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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&amp;nbsp;doing that -&amp;nbsp;&lt;A class="link-titled" href="https://developers.arcgis.com/labs/pro/analysis-with-python/" title="https://developers.arcgis.com/labs/pro/analysis-with-python/"&gt;Run a geoprocessing task and python script | ArcGIS for Developers&lt;/A&gt;. 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.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;I would think so, given the information given here:&amp;nbsp;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/mapping/arcgisproject.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/mapping/arcgisproject.htm"&gt;ArcGISProject—ArcPy | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;. Specifically, with regards to getting an ArcGISProject object:&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;EM&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;The second technique is to use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="background-color: #ffffff; font-size: 17px;"&gt;CURRENT&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;keyword as an input parameter to the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="background-color: #ffffff; font-size: 17px;"&gt;ArcGISProject&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function. This technique only works from within an&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="color: #4c4c4c; background-color: #ffffff;"&gt;ArcGIS Pro&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;application because the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="background-color: #ffffff; font-size: 17px;"&gt;ArcGISProject&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;object references the project that is currently loaded into the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="color: #4c4c4c; background-color: #ffffff;"&gt;ArcGIS Pro&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;application. The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="" style="background-color: #ffffff; font-size: 17px;"&gt;CURRENT&lt;/SPAN&gt;&lt;SPAN style="color: #4c4c4c; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;keyword can be used in the Python window or with geoprocessing script tools.&lt;/SPAN&gt;&lt;/EM&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;David&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Oct 2019 19:24:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/calling-python-scripts-from-c-add-in-code-and-vice/m-p/822530#M2892</guid>
      <dc:creator>DavidLaMartina</dc:creator>
      <dc:date>2019-10-30T19:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calling Python scripts from C# Add-In code (and vice versa)</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/calling-python-scripts-from-c-add-in-code-and-vice/m-p/822531#M2893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;David,&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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 &lt;A href="https://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/creating-a-new-python-toolbox.htm"&gt;Python toolbox&lt;/A&gt; 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 &lt;SPAN style="font-family: 'andale mono', monospace;"&gt;GeoProcessing&lt;/SPAN&gt; class and it would automagically work using the active Python environment. Things like the name or path location can be retrieved via &lt;SPAN style="font-family: 'andale mono', monospace;"&gt;Project.Current;&lt;/SPAN&gt; in C# and passed as arguments into the geoprocessing tool.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;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.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;Calling C# functionality from Python is more of a generic question and maybe something like &lt;A href="https://bitbucket.org/pydotnet/pydotnet/wiki/Home"&gt;PyDotnet &lt;/A&gt;could be worth exploring. I haven't used the module myself but it certainly looks interesting.&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;Hope this helps,&lt;/P&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;- Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Oct 2019 21:37:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/calling-python-scripts-from-c-add-in-code-and-vice/m-p/822531#M2893</guid>
      <dc:creator>ThomasEmge</dc:creator>
      <dc:date>2019-10-30T21:37:44Z</dc:date>
    </item>
  </channel>
</rss>

