Script tool not creating output, but works in Python Window

1363
10
Jump to solution
06-18-2021 10:04 AM
ChrisGAEG
Occasional Contributor

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! 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
ChrisGAEG
Occasional Contributor

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! 

View solution in original post

0 Kudos
10 Replies
DanPatterson
MVP Esteemed Contributor

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.


... sort of retired...
ChrisGAEG
Occasional Contributor

Thanks for the reply Dan. I do have aprx defined, but I accidentally didn't include in screenshot. Here is the full script. I still have the same issue where the spatial join is not happening. 

0 Kudos
ChrisGAEG
Occasional Contributor

Correction, the spatial join is happening it's just not being added to the map. I'll try to figure this out. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

makefeaturelayer .... needs to be added to the script... alternately, add it after


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...
0 Kudos
ChrisGAEG
Occasional Contributor

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. 

0 Kudos
ChrisGAEG
Occasional Contributor

I believe I fixed my imports as well. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

perhaps package a gdb with the toolbox located in the same path location as the toolbox, which is easier to locate


... sort of retired...
0 Kudos
ChrisGAEG
Occasional Contributor

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! 

0 Kudos