Select to view content in your preferred language

Calling a custom script tool within a loop

2189
7
03-10-2011 11:04 AM
EstherFernández_García
Esri Contributor
Hi everybody!

I'm having an issue calling a custom tool (python code) from another script tool. I need to loop through a bunch of folders and execute the custom tool for each one of them. If I call a model tool (from the same toolbox) inside the loop everything works fine but If I call the custom script tool it seems arcpy is not able to find the tool. The script tool calls to other custom modules.
I get the following error:

<type 'exceptions.AttributeError'>: Object: Tool or environment <s> not found

If I try a second execution the error is slightly different:

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'LectorASC_AdmLocal'

'AdmLocal' is the alias of my toolbox and 'LectorASC' the name of my python tool. This is the call I'm doing inside the loop:

arcpy.LectorASC_AdmLocal(DXFPolyline,subpar)

The weird thing is the first call in the loop works fine but the second throw the exception.

Any idea?

Thanks in advanced

Esther
Tags (2)
0 Kudos
7 Replies
StevePeaslee
Deactivated User
Esther,

This may not help in your situation, but I ran into a similar problem with 9.3.1. I finally added a few lines of code to to my 'looping script' to list all of the available tools (gp.ListTools and a 'for' loop to print the list).

In the printed list of tools, I noticed that the tool that I was calling within the loop was listed 3 times This was because I had added it to a couple of other toolboxes. Once I removed the other instances of the script tool from ArcToolbox, the problem went away.

-Steve
0 Kudos
StevePeaslee
Deactivated User
Esther,

Another thought. Are you using gp.AddToolbox inside your loop? That might cause problems.

One issue that I haven't figured out. Perhaps you can help me...

None of my gp messages are printing from the second script that is called from the first script with the loop. Do you have that problem?

-Steve
0 Kudos
EstherFernández_García
Esri Contributor
Thank you for your ideas.:D

I'm using arcpy.ImportTool(path_to_the_toolbox) to add the toolbox I need. I've imported at the beginning of the loop but I've tried several places and the problem persist.

About the messages I have the same problem, none of the them return to the loop script:(

Regards

Esther
0 Kudos
MichaelFaulcon1
Deactivated User
Experiencing the same problem.
I'm using 9.3.1 and gp.AddToolbox

Any news on this?
0 Kudos
StacyRendall1
Frequent Contributor
Your problem may be different, but we worked out a solution for something similar here.

Essentially, you are trying to import the tool by its name, however what a toolbox model is listed as in ArcCatalog/Map is actually just the label for the tool. You have to set the name by right clicking on it in Catalog, then under the General tab editing the Name field.

When a toolbox is imported, its models are added to the arcpy namespace (stuff you can access with the line arcpy.something) by their name (not label) plus the toolbox alias you assign when importing it.

If you open your toolbox in Catalog and make sure the name is set correctly, not just the label, your model should run fine.


You also have (in this particular case, calling one Python code from another) the option to skip the Arc toolbox bits all together, which would probably increase reliability and speed, by calling the second Python directly script from the first. If you want more information on this let me know; I am planning on posting a tutorial on how to do this on my blog soon, but I'll try to get it done more quickly if there is a demand.

Let me know how you get on!
0 Kudos
MichaelFaulcon1
Deactivated User
All tools in toolbox run fine the first time thru the loop.
It is only on subsequent calls that I get an error "Tool or environment xxx not found"
0 Kudos
StacyRendall1
Frequent Contributor
You will have to post your code (or the most simplified version you can that still produces the error) before I can help more...
0 Kudos