# Setting in memory workspace in ArcPy?

Is it possible to set env.workspace = "in_memory" in ArcPy using both ArcGIS Pro and the ArcGIS 10.2.2 (or 10.3) architectures?

What I am trying to do is get the output of a snap pour point operation written to memory as opposed to disk.

I realize I could write to disk then bring it into memory but this would not help. I am trying to optimize a series of tasks minimizing the use of writing to physical media the output of a process that is merely required as an input to the next process.

I'm going to throw an answer on here because both answers thus far aren't 100% correct.

There are 2 items which can vary from tool to tool.

1. if it honors the workspace environment (this item is always documented on the tool help page)
2. if it can make use of the in_memory workspace (this item may not be explicitly documented. You're more likely to see a note if it DOES NOT support in_memory)

To simply answer the "can you set the environment workspace to in_memory". The answer is YES.

>>> import arcpy
>>> arcpy.env.workspace = r"in_memory"
>>> arcpy.CopyFeatures_management(r"c:\temp\foo.shp", "myinmemoutput")
<Result 'in_memory\\myinmemoutput'>
>>> arcpy.Exists("myinmemoutput")
True


Snap Pour Point does honor the workspace environment per it's documentation and explained Python samples. And a test shows you can write output to in_memory and work with that variable reference...to put into another tool, or save the result

>>> import arcpy
>>> arcpy.env.workspace = r"in_memory"
>>> arcpy.CheckOutExtension("SPATIAL")
u'CheckedOut'
>>> snapOut = arcpy.sa.SnapPourPoint("e:/gpservices101/hydro/US30m/test.gdb/sourcepoint", "e:/gpservices101/hydro/US30m/Region08a/Input/elev_cm", 1,"PourPtID")
>>> snapOut
in_memory\SnapPou_sour1
>>> arcpy.Exists(snapOut)
True
>>> snapOut.save(r"c:\temp\todisk.tif")
>>> arcpy.Exists(r"c:\temp\todisk.tif")
True


Unfortunately, I don't think setting the workspace to the in_memory location like in your question is possible; In such a way that geoprocessing tools will default their output locations to the in_memory workspace.

I just now tried to set the workspace to '\\in_memory' myself, and tested it with a simple CopyFeatures_management() tool run. The string representation of arcpy.env.workspace is indeed set to '\\in_memory', but I'm thinking the CopyFeatures_management() tool is not respecting the workspace environment setting in this case.

It would be awesome though, if we could set in_memory as the default workspace at the map document level. Maybe you could raise this as an ArcGIS Idea?

Best Luck.

EDIT:

Hey I was thinking also since you are using the Snap Pour Point Tool, we may need to approach this a different way. Snap Pour Point appears to act directly on the pour points feature class and does not create an output feature class as part of the tool result. Could we create a temporary, in_memory copy of the pour points and work from there?

EDIT to EDIT:

Oh true, ok you've mentioned you realize this already - sorry.

• I may do that (ArcGIS idea). I wonder why people would vote down this question? I agree with you Jim it would be awesome. Oct 9 '15 at 18:26
• Ah I get the question now. Yes Jim is correct, setting the workspace like that does nothing. You have to specify in_memory for your individual processes. You cannot get it to default with that behavior. Oct 9 '15 at 18:27
• The down vote may have come from the short-length of your question. I see what you mean though - maybe edit your question a bit with some more context and others will vote it back up?
– Jim
Oct 9 '15 at 18:27
• Ok, It is such a direct question I did not see a need to fluff it up. I will do as you say. Oct 9 '15 at 18:29
• reworded question to see if another way exists. Oct 9 '15 at 18:32