Script won't run outside of ArcMap

1504
4
Jump to solution
11-18-2015 08:47 AM
MichaelRiccio1
New Contributor

Hello, I am fairly new to Python, but I'm having an issue running a script. It runs fine when I use the python window in ArcMap, but when I try to run the same script either from double clicking a .py file, or running in the IDLE window, I get an error. I know I can easily do this just using select by attributes in ArcMap, but I'm trying to learn more python to automate more things down the road, this is a first test.

The Code:

import arcpy

import os

tree = r"M:\\Temp\ArcPy\Tree.shp"

arcpy.env.overwriteOutput=True

arcpy.MakeFeatureLayer_management(tree, "Tree")

arcpy.SelectLayerByAttribute_management("Tree","NEW_SELECTION",' "Diameter" >=50')

arcpy.CopyFeatures_management ("Tree","Trees50DBH")

arcpy.FeatureClassToFeatureClass_conversion("TreesGreater50DBH",r"M:\\Temp\ArcPy","Tree50DBH")

The Error (happens at the arcpy.CopyFeatures_management code)

Traceback (most recent call last):

  File "M:\Temp\ArcPY\TreeArcPyTest.py", line 12, in <module>

    arcpy.CopyFeatures_management ("Tree","Trees50DBH")

  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 2434, in CopyFeatures

    raise e

ExecuteError: ERROR 000210: Cannot create output Trees50DBH

Failed to execute (CopyFeatures).

I tried searching the various solutions here and elsewhere, but largely they don't apply? Thanks for your help.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Regular Contributor II

I'm guessing your problem is with workspaces. In the call to the CopyFeatures tool you're specifying the name of the output class without the directory. Try giving the following a shot. I'm assuming you want to create shapefiles, so I added the extension to the name of the output file. I'd assume in arcmap that everything would output to the default.gdb.

import arcpy

import os

tree = r"M:\Temp\ArcPy\Tree.shp"

arcpy.env.overwriteOutput = True

arcpy.env.workspace = os.path.dirname(tree)

arcpy.management.MakeFeatureLayer_management(tree, "Tree")

arcpy.SelectLayerByAttribute_management("Tree","NEW_SELECTION",'"Diameter" >=50')

arcpy.CopyFeatures_management ("Tree","Trees50DBH.shp")

arcpy.FeatureClassToFeatureClass_conversion("TreesGreater50DBH", r"M:\Temp\ArcPy","Tree50DBH.shp")

View solution in original post

4 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

you can start by putting an "r" before you path assignment

tree = r"M:\\Temp\ArcPy\Tree.shp"

also, check out Posting Code blocks in the new GeoNet​  for info on posting/formatting your code.  Makes it easier for others to read, and helps with knowing if you space/tabs are lined up.

MichaelRiccio1
New Contributor

Thanks for the formatting tip. I have updated the post.

I also added the r in front of the path assignment, but result is unchanged.

0 Kudos
FreddieGibson
Regular Contributor II

I'm guessing your problem is with workspaces. In the call to the CopyFeatures tool you're specifying the name of the output class without the directory. Try giving the following a shot. I'm assuming you want to create shapefiles, so I added the extension to the name of the output file. I'd assume in arcmap that everything would output to the default.gdb.

import arcpy

import os

tree = r"M:\Temp\ArcPy\Tree.shp"

arcpy.env.overwriteOutput = True

arcpy.env.workspace = os.path.dirname(tree)

arcpy.management.MakeFeatureLayer_management(tree, "Tree")

arcpy.SelectLayerByAttribute_management("Tree","NEW_SELECTION",'"Diameter" >=50')

arcpy.CopyFeatures_management ("Tree","Trees50DBH.shp")

arcpy.FeatureClassToFeatureClass_conversion("TreesGreater50DBH", r"M:\Temp\ArcPy","Tree50DBH.shp")

View solution in original post

MichaelRiccio1
New Contributor

Thanks a lot, this has made it work. In the future I'll keep the workspace issue in mind and add any file extensions.

0 Kudos