I'm having issues using arcpy and spatial analyst functions. The workspace environment seems to get selectively ignored. I am running this code directly in the Python window in ArcGIS Pro 2.5.1. The behavior appears the same in an existing project versus a brand new unsaved project.
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">as</SPAN> ap
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN>
testing <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\Desktop\Prop\Prop_Slope.gdb"</SPAN>
dem <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"F:\5_Tools\SlopeClass\AARM.gdb\DEM_AOI"</SPAN>
demfs1 <SPAN class="operator token">=</SPAN> FocalStatistics<SPAN class="punctuation token">(</SPAN>dem<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Circle 5 CELL"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">)</SPAN>
demfs2 <SPAN class="operator token">=</SPAN> FocalStatistics<SPAN class="punctuation token">(</SPAN>demfs1<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Circle 5 CELL"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">)</SPAN>
demfs3 <SPAN class="operator token">=</SPAN> FocalStatistics<SPAN class="punctuation token">(</SPAN>demfs2<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Circle 5 CELL"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#with arcpy.EnvManager(workspace = env.workspace):</SPAN>
Contour<SPAN class="punctuation token">(</SPAN>demfs3<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Contour"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
remap <SPAN class="operator token">=</SPAN> <SPAN class="string token">"0 19.999 1;20 49.999 2;50 10000 3"</SPAN>
rclass <SPAN class="operator token">=</SPAN> Reclassify<SPAN class="punctuation token">(</SPAN>demfs3<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"VALUE"</SPAN><SPAN class="punctuation token">,</SPAN> remap<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NODATA"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#with arcpy.EnvManager(workspace = env.workspace):</SPAN>
ap<SPAN class="punctuation token">.</SPAN>conversion<SPAN class="punctuation token">.</SPAN>RasterToPolygon<SPAN class="punctuation token">(</SPAN>rclass<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Reclass_Polygon"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SIMPLIFY"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"VALUE"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"SINGLE_OUTER_PART"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> testing<SPAN class="punctuation token">:</SPAN>
demfs1<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"FS1"</SPAN><SPAN class="punctuation token">)</SPAN>
demfs2<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"FS2"</SPAN><SPAN class="punctuation token">)</SPAN>
demfs3<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"FS3"</SPAN><SPAN class="punctuation token">)</SPAN>
rclass<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Reclassified"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> demfs1<SPAN class="punctuation token">,</SPAN> demfs2<SPAN class="punctuation token">,</SPAN> demfs3<SPAN class="punctuation token">,</SPAN> rclass<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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The code itself executes without a hitch, however the outputs get saved in different locations. Calling the .save() method on the returned raster objects will create permanent saves in the location specified in "env.workspace". However, both the outputs of "Contour" and "RasterToPolygon", get saved to the Default geodatabase for the project (either a named default, or just the "Default.gdb" in an unsaved project).
Using the currently commented-out "EnvManager" and "with" blocks corrects this problem. Why does it appear necessary to explicitly define the workspace for these tools, when I have already done it globally at the top? What aspect of the workspace environment am I missing? Perhaps the use of spatial analyst tools here is coincidental, but it's when using these tools that I tend to experience strange behavior with the workspace environment.
Any help would be greatly appreciated.