Error 000732 when trying to use GetParameterAsText in python script tool

2772
15
08-30-2016 09:30 AM
AndrewRowlands
New Contributor

I'm attempting to write code to run a buffer and drive time analysis on an XY data file and then count features from another shapefile within the drive time trade area that's created. I'm attempting to use the Spatial Join (Analysis) for the latter process. I'm currently having an error with my code at the Spatial Join part of the code. It says the dataset "false" does not exist, which is not a dataset that I'm trying to use or am even aware of. Here is the error message with the key details:

Traceback (most recent call last):
File "Y:\BI\GIS Data\GIS Data Backup\Python Scripts\InternalModel_PythonScript_SingleSite.py", line 121, in <module>
arcpy.SpatialJoin_analysis(targetFeaturePoly, joinSpatialConvStores, outPolyCStorePoints)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\analysis.py", line 471, in SpatialJoin
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Join Features: Dataset false does not exist or is not supported
Failed to execute (SpatialJoin).

I will say, this script runs properly when I do not see the final parameter, but instead link directly to the file path in the code. It's once I try to use GetParameterAsText when the error occurs. Here is the code attached. The error is occurring in this section. When I remove the comment from line 3 and run the script directly setting that variable to the shapefile, it works fine.

#create layers for summarize within function; pull number of c-stores within drive time polygon
joinSpatialConvStores = arcpy.GetParameterAsText(2)
#joinSpatialConvStores = r"X:\Users\Andy.Rowlands\AHR.Data\MPSI_KalibrateData\MPSI_Competition_Locations\2015Surveys\DesMoines_2015_locations.shp"
targetFeaturePoly = fcDriveTimeAppend
businessPointsString = "CStoreCount_"
outPolyCStorePoints = businessPointsString + fcLocation
arcpy.SpatialJoin_analysis(targetFeaturePoly, joinSpatialConvStores, outPolyCStorePoints)
print ("CStore Count Done")
0 Kudos
15 Replies
DanPatterson_Retired
MVP Emeritus

the Get.... things are numbered from zero... try not to mix and match... I will defer to xander_bakker for commentary since I always use sys.argv[ ], where sys.argv[0] is the running script and your 0's are my 1's (aka, the parameters of whatever flavor are offset by 1

0 Kudos
XanderBakker
Esri Esteemed Contributor

If you look at the example below:

... you see 4 parameters. To get the one called "Clipshape", you will have to use index = 0, "Workspace" in this case will have index = 3.

You can also look at this thread that explains why it was necessary in this particular case to get the parameter as an object and not as text: https://community.esri.com/message/631100-re-passing-arcmap-layers-to-a-python-script-inside-arctool... 

AndrewRowlands
New Contributor

Okay, that makes sense and I've set up my GetParameters with the proper setup and with the correct order. However, still having an issue with that final GetParameter running as an error message. Here's the code. Line 3 is what I'm putting as the parameter in line 2, and it's failing. However, if I remove the comment from line 3 and run the script that way with no final GetParameter, it's still running fine.

#create layers for summarize within function; pull number of c-stores within drive time polygon
joinSpatialConvStores = arcpy.GetParameter(2)
#joinSpatialConvStores = r"Y:\BI\KalibrateCompetitionLocations\MPSICompetitionLocations\2015Surveys\DesMoines_2015_locations.shp"
targetFeaturePoly = fcDriveTimeAppend
businessPointsString = "CStoreCount_"
outPolyCStorePoints = businessPointsString + fcLocation
arcpy.SpatialJoin_analysis(targetFeaturePoly, joinSpatialConvStores, outPolyCStorePoints)
print ("CStore Count Done")
0 Kudos
DanPatterson_Retired
MVP Emeritus

you have it as a featurelayer... I assume you have it loaded in arcmap?  It would be interesting to see the dialog with the parameters specified.  If it is working with a hardcoded path and not as a feature layer, then it doesn't have enough information to locate it on disk, hence, you would have to code a means to complete the path or change your input data type from featurelayer.

0 Kudos
AndrewRowlands
New Contributor

Can you say a bit more on this? Isn't the feature layer/shapefile that's selected in the GetParameter dialog box just a file path to the shapefile? Or is it read a different way?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Some background
Adding a script tool—Help | ArcGIS for Desktop 

plus others... I will let you look up the links in the same area

    Setting script tool parameters—Help | ArcGIS for Desktop
    Accessing parameters in a script tool—Help | ArcGIS for Desktop

    etc
But in summary, when you are working within arcmap and set your environment workspace and select a layer as input to a tool, the script will work with the layer and use any selection placed upon it. If you run that same script and don't provide a full working path to the file, either by setting the environments or directly providing it. The script has no clue where the file is located.
The best way is to look at the options available to you when you specify a feature layer, a feature class etc. Some parameters have a little black drop down arrow beside a folder icon. if you select from the list provided by clicking on that arrow, you can choose from layers within arcmap. Alternately, if you click on the folder icon, you get to navigate to the file on disk.

Consider the scenario... you have a layer in arcmap with a selection... you decide to run some tool... if that tool uses the layer within arcmap, only the features selected process will be used by the tool.
But!!! Damn you say... I want to use this tool, but I don't want to undo the selection... what to do??? simply click on the folder icon, navigate to that file on disk and the process will be run on the whole file REGARDLESS of the selection in ArcMap... cool trick... or a stumbling block.
What to do? add print statements (for pure python code running) and arcpy.AddMessage statements (for toolbox reporting. Check what you have. If you think you have a file on disk, but all that is reported is the file's name without the path, then you know you have a problem.

The only way to figure out what is what... is to try it, giving the rules are easily forgotten... as for the messaging

in_fc = arcpy.GetParameterAsText(0)
msg = "Input paramater 1... { }".format(in_fc)
print(msg)
arcpy.AddMessage(msg)
‍‍‍‍

You can make that print/addmessage into a little def if you want

def stuff(msg):
    """print stuff for python and/or toolbox scripts"""
    print(msg)
    arcpy.AddMessage(msg)
‍‍‍‍

then you just call it

stuff(msg)

that is all for now

0 Kudos