Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a python script and am stuck on the first obstacle. I want the user to select features in ArcMap and then use these selected features in a python script. Is that at all possible and does someone have a code example? The code that I have now uses all the features instead of just the selected ones. Please see code snippet below.

pipesnew = os.path.join(ws,"pipesnew")

arcpy.CopyFeatures_management("pipeslyr", pipesnew)
share|improve this question
I am trying to do something similar but am stuck on how to make sure the user selects features before the script is run. Would this be a script parameter, and if so which data type? – kflaw Jul 24 '14 at 15:37
up vote 8 down vote accepted

arcpy.CopyFeatures_management works like right-clicking on the layer > export data > selected features. Then you can make the layer from that exported selection. I'd do this "in_memory" so you don't have deal with overwriting each time you run the script.

import arcpy
from arcpy import env

arcpy.env.workspace = "in_memory"

selected_features = "The Feature Class with the selection.shp"
pipes = "the new feature class to convert to a layer"

#this will create a new feature class from the selected features but will do it In Memory
arcpy.CopyFeatures_management (selected_features, pipes)

#Now do all the other stuff you want like convert it to a layer and work with it


BTW, the selection would need to be made before this is run.

share|improve this answer
Thank you! That works. In addition to changing my code to include copy features, I had to change the parameter settings in Toolbox as well. My input is now a feature layer instead of a feature class. – Kat Jun 17 '13 at 21:34


As your script stands if you want to copy out a selection then the selection must exist on the layer, but you would need to get a handle on the layer from your mxd first. Have a look at this page about referencing a layer in an mxd.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.