1

I have a python script that works correctly when used in a toolbox but not from the command line. How do I duplicate the toolbox magic that happens when specifying a parameter Data Type?

There is GetParameterAsText() but apparently not GetParameterAs('FeatureLayer'), or GetParameterAs('Workspace'), ... in the GetParameter docs.

The parameters as handled in the script:

mxd = arcpy.GetParameterAsText(0)
clip_layer = arcpy.GetParameterAsText(1)
out_gdb = arcpy.GetParameterAsText(2)

In the toolbox I have the 2nd with Data Type set to Feature Layer.

Tool parameters screenshot

When run from the command line this emits:

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.

...while from the Tool it is:

Executing: clipAllLayers Q:\Current_AOI.mxd Q:\current_aoi.gdb\Converted_Graphics Q:\clipped.gdb
Start Time: Wed Feb 25 10:33:30 2015
Running script clipAllLayers...
layer_one
layer_two
...snip...
Completed script clipAllLayers...
Succeeded at Wed Feb 25 10:33:55 2015 (Elapsed Time: 24.81 seconds)

  • 2
    You might try changing Feature Layer to Feature Class in the tool parameters. – ianbroad Feb 25 '15 at 19:29
  • You can also use argparse to set command line arguments and options. – recurvata Feb 25 '15 at 19:50
  • 1
    What tool are you passing "clip_layer" too? It sounds like that tool is specifically looking for a layer when you've given it a featureclass. – KHibma Feb 25 '15 at 20:20
  • @KHibma: if lyr.isFeatureLayer: arcpy.Clip_analysis(lyr,clip_layer,out_layer), whole script coming shortly. – matt wilkie Feb 25 '15 at 22:21
3

I figured it out! My question was misguided and sent everyone in the wrong direction, sorry for the confusion folks.

The error was that the tool parameters and the command line parameters were not, after all, precisely the same. The "magic" is that when using a Toolbox all paths are converted to fully qualified paths, ..\source\data.gdb\some_layer --> X:\path\to\source\data.gdb\some_layer, before landing at the script.

When I use full paths from the command line, it's all good.

Also, just to make debugging a real pain, an identically worded error message occurs when a full path parameter is used, but the source mxd has a broken layer. :-/

Update: Relative paths do work from console, however the important part is relative to _what_!

When invoking arcpy.mapping.MapDocument the current workspace switches to the same folder as the .mxd. This probably happens elsewhere in arcpy too. I was operating on the usual command shell convention that paths are relative to the current directory.

In any case the moral of the story remains: always use full paths, or add logic to scripts to expand paths for you and save on typing.

  • Aside: I cautiously added some debugging info to upstream C:\ArcGIS\Desktop10.3\arcpy\arcpy\analysis.py, and discovered the 1st and 3rd arguments are Unicode encoded while the 2nd is not. It looks like this is an omission and a bug waiting to manifest, but doesn't appear to have any bearing on what I encountered here. The addition: print '\nDEBUG: ',(in_features, clip_features, out_feature_class), '\n' at line 53, between try: and retval = .... Result: _"DEBUG: (<map layer u'Clip_Poly'>, 'current_aoi.gdb\\Clip_Poly', u'scratch\\clipped\\clipped.gdb\\Clip_Poly')" – matt wilkie Feb 26 '15 at 17:56
  • and here's the script that started it all: github.com/maphew/arcplus/blob/master/ArcToolbox/Scripts/… – matt wilkie Feb 26 '15 at 18:02
2

When running your tools from the console it is suggested you use the more general sys.argv instead of the arcpy-specific arcpy.GetParameterAsText(). This page from Esri shows you how to use sys.argv. Change all of your inputs to this, and you should be able to run it from the console.

mxd = sys.argv[1]
clip_layer = sys.argv[2]
out_gdb = sys.argv[3]

Also check out this great answer for more information on this topic.

  • 1
    I always use sys.argv[n] and have not had any problems. It's also much less typing. You still do need to be careful with spaces though, any path with a space (like c:\program files) needs to be quoted ("c:\program files\") or the script will see two arguments (c:\program and files). – Michael Stimson Feb 25 '15 at 21:43
  • Using sys.argv doesn't change things. There's something different about what is passed to the script when it run from a toolbox than from command. I'll add some more details and put everything online somewhere. – matt wilkie Feb 25 '15 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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