Select to view content in your preferred language

Running Python Scripts from Add-In and Stand-alone Python Environments

157
4
Wednesday
Vidar
by
Frequent Contributor

Hi,

I’m hoping to get some insight or advice from the community on an issue we’re encountering with a custom ArcGIS Pro Add-in developed in C#.

Software Stack:

  • ArcGIS Pro 3.2.4 (Advanced + Spatial Analyst)

  • Windows 10 Pro (Build 19045.5737)

  • Visual Studio 2022 (v17.13.6)

  • Python environments: arcgispro-py3 (default) and a separate standalone Python 3.9 installation (required for pygplates)

     

    Objective

    We have a custom Add-in button that runs two Python scripts in sequence:

    1. Script 1 runs using the ArcGIS Pro environment (propy.bat).

    2. Script 2 runs in an external standalone Python 3.9 environment (due to dependency conflicts with pygplates).

    3. Script 1 runs again for some arcpy based clean up and other misc stuff.

      This all works well until the user manually adds a raster to the map (via Map → Add Data → Data). After doing this, the second script fails when executed via the standalone environment.  This is not the only thing that causes this problem - if the user opens up the interactive Python prompt in Pro - that causes the same issue.

      Symptoms

      After adding a raster in the Pro UI, attempts to run the tool again affects the external Python script fail with errors like:

       
      Fatal Python error: init_import_site: Failed to import the site module ... TypeError: type 'types.GenericAlias' is not an acceptable base type

       

      If we run the same scripts from a standalone .NET Console app (i.e., not from within Pro), the scripts work perfectly, so I know it's nothing to do with scripts themselves.  The same scripts also work perfectly if run within a Python Toolbox - its only if the scripts are triggered from C# using the System.Diagnostics.Process instance.

      See code example below:

public static string RunPythonScript(string scriptFileName, string[] scriptArguments, string pythonPath)
{
string scriptPath = Path.Combine(@"D:\Projects\FooProject\PythonScripts", scriptFileName);

if (!File.Exists(scriptPath))
return "Python script not found.";

if (string.IsNullOrWhiteSpace(pythonPath))
pythonPath = GetPythonPath();

if (string.IsNullOrWhiteSpace(pythonPath))
return "Python environment not found.";

var args = new StringBuilder($"\"{scriptPath}\"");
if (scriptArguments != null)
{
foreach (var arg in scriptArguments)
args.Append($" \"{arg}\"");
}

var startInfo = new ProcessStartInfo
{
FileName = pythonPath,
Arguments = args.ToString(),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};

using var process = new Process { StartInfo = startInfo };
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

return output;
}

Question to the Community

Has anyone else encountered issues with the Pro environment interfering with external Python script execution from within a C# Add-in?

  • Is there a known workaround for running Python scripts in multiple environments from a Pro Add-in?

  • Could Pro be “locking” or initializing some internal Python state that affects subprocesses? I appreciate this will probably be something to do with Conda and the internally installed Python environment Pro installs.

  • Is there a recommended pattern for isolating or sandboxing these environments in a Pro-based C# add-in workflow?

    Why It Matters

    Our workflow depends on Python packages that cannot be installed in the default ArcGIS Pro environment (e.g., pygplates). Being able to reliably switch environments from a Pro Add-in is essential for our tool to function correctly and do the calculations we need to do with pygplates. Currently, interaction with the native Pro UI (like adding rasters) breaks that workflow and puts pro into some locked state with the default Python environment.

    Would appreciate any insights, workarounds, or confirmation if others have seen similar behavior.

    Thanks in advance!

Tags (2)
0 Kudos
4 Replies
RichardDaniels
Honored Contributor

ArcGIS Pro supports different python containers (see DeepLearning with ArcGIS Pro discussions re: cloned environment of ArcGIS Pro's default environment,).  The scripts would need to be explicitly told what container they should run in. Sounds like when you load a Raster the Spatial Analyst extension is being, for lack of a better word, Turned-On. This is (re)setting the _conda_ python path back to the default used by SA.

See - Install and Setup | ArcGIS API for Python

 

*By default, ArcGIS Pro has a single conda environment, arcgispro-py3, which includes all Python libraries used by ArcGIS Pro as well as several others, such as scipy and pandas. Enviornments can be activated in Pro via File | Package Manager.  I don't know how to do this in C#, but you need to replicate this Activated logic to swith between your Python scripts.

0 Kudos
Vidar
by
Frequent Contributor

Hi Richard, thanks for the answer - I will research the first part of your reply. However I think programmatically activating environments in Pro - would be problematic (even if its possible) as you have to restart Pro which would obviously disrupt the user experience and render the add-in unusable.

0 Kudos
GhislainPrince
Esri Contributor

Hi Vidar,

 could you try to modify your code to have `ProcessStartInfo` with `UseShellExecute=True` . Just curious if that has positive impact.

0 Kudos
Vidar
by
Frequent Contributor

I will have a try - when I've uncorrupted my python environments with Pro.  😞

0 Kudos