Select to view content in your preferred language

Calling Multiple Python Scripts in One Tool

2287
3
07-14-2010 07:38 AM
JeremyKoontz
Emerging Contributor
Good morning everyone!

I've been working on a script that is supposed to automate a task, and I currently split up all of the steps into their own tools.  The issue that I'm having is when I try and run the script tool in ArcGIS.  Currently it works fine in the command line, but when I load the script tool into a Toolbox in Arc, it seems to just go past the subprocess calls without actually doing them.  It looks like it is trying to spawn off a seperate python process (Task Manager shows that there is a python.exe running when the script starts) but it closes quickly.  I use the python "subprocess" module (the .call function) to call each file, but I have also used the obsolete "os.system" call with no effect.  I have also tried to set a return value to the subprocess call to try and halt the program from going further until the program has returned a value, but that has not helped either.  I've also tried checking and unchecking "Run in Process" for the script too, and to no avail.  The only thing that starts when I don't run the script in process is another instance of PythonWin, and it quits soon after.

In my programs I have used os.system to call some .aml files that I have created, and those work fine when only running one tool.  But I believe the issue must be with calling multiple files in this script.

My question can be simplified to just this:  How do you call other script tools (python files) inside of a script, and how do you make sure that the second process will wait for the first process to complete before it continues?

Here is a snippet of the code that I am using, I won't post the whole thing because the other steps are the same format as this one, just different filenames and parameters.

subprocess.call ( "python " + pypath + "\\ThiessenCatchments.py " + gp.workspace )


Thank you for your time, and if you need any additional information, I will more than likely be glad to oblige!
0 Kudos
3 Replies
ChrisSnyder
Honored Contributor
Sounds as if your subprocess script is failing immediately after it is called. It might be useful for it to write some sort of log file so you can see how far it gets. for calling scripts from a "parent" script I have always used the old fashioned os.spawnv. For example:


rootDir = r"D:\temp"
script = r"\\snarf\div_lm\script.py"
dateTimeStamp = time.strftime('%Y%m%d%H%M%S')
for path in sys.path:
    if os.path.exists(os.path.join(path, "python.exe")) == True:
        pythonExePath = os.path.join(path, "python.exe")
parameterList = [pythonExePath, script, rootDir, dateTimeStamp[:8]]
os.spawnv(os.P_WAIT, pythonExePath, parameterList)
#or to not wait for the spawned process to finish...
os.spawnv(os.P_NOWAIT, pythonExePath, parameterList)


If you figure out how to get the subprocess module to use a "no wait" method, can you post it? I never found a way to do it...
0 Kudos
JeremyKoontz
Emerging Contributor
Chris,
  Thank you for your help.  For some reason, using your way didn't help me at all (but that's not your fault!)  When I used spawnv, it popped the command line windows up, but nothing ever happened and they immediately quit running after they were spawned.  This was with os.P_WAIT too!

But I would like to thank you because your snippet of code gave me the idea that I was calling python wrong!  When I used the command line to call the script, it would use my environment variables, of which the path to python.exe is included.  So, calling "python" in the command line brings up the interactive window and I'm able to use that variable in my scripting.

But (and this baffles me quite a lot) somehow, even though Python is INSTALLED with ArcGIS, it doesn't seem to be able to find it when I call "python".  I hope you don't mind that I used your method of finding the python.exe file on my computer and using that in place of my "python" call.  By doing that, os.system ( ) works correctly and subprocess.call ( ) works as well by using the pythonExePath.  Now for some reason Arc doesn't appreciate my first script, and is throwing an "Attribute Error: Object: Environment <workspace> not found."  Think anyone can shed a little light on that error?  I've never gotten it before and the code works fine when it is run in the command line.

Thanks again Chris, I appreciate your help, and I hope that I might have helped you a bit as well!

Jeremy
0 Kudos
jamessample
Emerging Contributor
Attribute Error: Object: Environment <workspace> not found.


Hi Jeremy,

Did you ever find out what was causing this error message? I'm having a very similar problem at the moment:

http://forums.arcgis.com/threads/17831-Attribute-Error-Object-Environment-lt-workspace-gt-not-found?...

Any advice would be much appreciated. Cheers!
0 Kudos