Select to view content in your preferred language

Calling Python Script from C# Add-in Button - ArcGISProject('Current') Not Working

07-05-2023 01:44 PM
Occasional Contributor

I have ran into a problem while trying out the ESRI sample "CallScriptFromNet".  I have my own scripts that I would like to try but they seem to get stuck when I create a "'current')" variable.  I get this message:


The script being referenced in C#:


The code works fine in Pro's Python window and its the correct parameter for that variable to work.  I am using this line of code so I can get a list of layers, check if they are properly named, etc...

Any ideas what I should do?

0 Kudos
1 Reply
Occasional Contributor

Since you're calling the script via System.Diagnostics.Process, it's being run outside the Pro environment, meaning that "current" doesn't mean anything. You have two options, depending on whether you want to continue following the sample or run the script within the Pro environment. I've done both and personally prefer the latter option, even though there's a little more setup.

To continue with the sample, you'll need to add an argument to your Python script and pass the value of `ArcGIS.Desktop.Core.Project.Current.Path` to it. That value will then replace "current".

To run the script within the Pro environment, you'll need to move the code to a Python toolbox (pyt) file and include it in your add-in. From there you can call it just like a geoprocessing tool, and "current" will continue to work.