Hello all and thanks in advance for reading through my question,
I'm writing a python script that is selecting features from a GDB feature class and using the attributes from the selected features during my analysis. I'm eventually displaying a Tkinter form to the user that displays the results. My script works fine if I run it outside of ArcMap in a command prompt but if I run it inside as a script tool (out of process) it fails.
I have worked through launching a script from a toolbox and running it "out of process" (unchecked Run Python script in process) to display the Tkinter form. As long as I don't need to use MakeFeatureLayer_management, my form launches, displays results, and behaves as expected. But since I need to create a layer from the GDB feeature class to be able to select features, I am running into the problem. The resultant layer from MakeFeatureLayer_management causes the script to fail if it is run out of process which is needed if I want to display a Tkinter form.
The code below should be able to recreate the problem I am having.
- Run it from the command line - prints the spatial reference name twice
- Run from a script tool with "Run Python script in process" checked - outputs the spatial reference twice
- Run from a script tool with "Run Python script in process" unchecked - crashes on line 8
<SPAN class="keyword token">import</SPAN> arcpy
myGDB <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\Data\myGeoDatabase.gdb'</SPAN>
polyFeatureClass <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Polys1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Polys2'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN>len<SPAN class="punctuation token">(</SPAN>polyFeatureClass<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
polyLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>myGDB <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\\'</SPAN> <SPAN class="operator token">+</SPAN> polyFeatureClass<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> polyFeatureClass<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
sr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>polyLayer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>spatialReference
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>sr<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>sr<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Any thoughts on what might be causing this and how I might be able to get around it? Is there another method to select feature from the feature class? I read what appeared to be a similar question posed in 2014 (https://community.esri.com/thread/104854) but the end solution was to run it outside of ArcMap. I really want my users to be able to use the tool within ArcMap.
Thanks,
Damian