I have a simple script that works perfectly fine in the python window in Pro. However when I import the script to a tool, the process fails on the first geoprocess line. I don't understand why this is happening or why running from a tool differs from the python window in this case. I attached a screen shot of the script. Any response is really appreciated!
Solved! Go to Solution.
It worked when I added the following lines -
workspace = a.env.workspace
lyr = arcpy.management.MakeFeatureLayer(workspace+"\\DropFiber_SpatialJoin", "DropFiber_SpatialJoin").getOutput(0)
maps.addLayer(lyr, 'TOP')
I have the workspace a parameter that the user sets before running the tool. This way the tool should work for co workers I send to. Fingers crossed!
It fails on the first line since the script tool has no clue what the aprx is.
Your import statements need to be at the top of the script before any of your required definitions of variables.
Correction, the spatial join is happening it's just not being added to the map. I'll try to figure this out.
makefeaturelayer .... needs to be added to the script... alternately, add it after
What is the error message Chris?
I am used to conventional tools that require that you define your parameters in the parameter dialog. The first line of the script would indicate instant failure since the tool wouldn't have a clue what "CURRENT" is but would require a path to an aprx.
Perhaps the error message would help to separate out the issue. I can see it working within pros' python window or pros' notebooks, but not as a conventional tool or a script tool (unless you have the parameters defined in the tools' script)
Currently there is no error message. It produces an excel of the spatialJoin table but it skips the select by attributes part because the join layer is not added to the map. If I add the lines below it, it adds to map and makes the right excel export. But I need to find out how to make that work if I send the tool to someone else.. The path to the layer for makefeaturelayer has to be a parameter set by the user maybe..
lyr =
arcpy.management.MakeFeatureLayer(r"Path_to_Layer\\DropFiber_SpatialJoin", "DropFiber_SpatialJoin").getOutput(0)
maps.addLayer(lyr, 'TOP')
Again, thanks for your help.
perhaps package a gdb with the toolbox located in the same path location as the toolbox, which is easier to locate
It worked when I added the following lines -
workspace = a.env.workspace
lyr = arcpy.management.MakeFeatureLayer(workspace+"\\DropFiber_SpatialJoin", "DropFiber_SpatialJoin").getOutput(0)
maps.addLayer(lyr, 'TOP')
I have the workspace a parameter that the user sets before running the tool. This way the tool should work for co workers I send to. Fingers crossed!