Toolbox script tool to call a 2nd script

565
7
Jump to solution
11-30-2018 10:30 AM
Lake_Worth_BeachAdmin
Regular Contributor

I have a basic Script Tool that works 100% of the time. 

I recently wanted to add a final function that would call a 2nd python script after the script tool is completed. The 2nd script is not a toolbox script just a standalone python file. 

I used:

os.system('script.py')

I get no errors after running the script with the new function but it doesn't appear the 2nd script is being called successfully. 

note: I did put the script.py file in the same directory as the toolbox script

Is this a limitation to ArcGIS Toolbox scripts or am I missing something? 

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Try something like:

script_stdout_output = subprocess.check_output([os.path.join(sys.exec_prefix, 'python.exe'),'your_2nd_script.py','arg1', 'arg2', '...', 'argN'])
‍‍‍

View solution in original post

7 Replies
Luke_Pinner
MVP Regular Contributor

os.system is not the best way to run another process, that's what the subprocess module is for.  However, I wouldnt reccommend this for running python scripts, It's much easier and more flexible to turn your script into a module, i.e wrap your script in a function, import the module, then call the function. Can't go into detail as I'm typing this on my phone.

0 Kudos
Lake_Worth_BeachAdmin
Regular Contributor

the issue with wrapping my 2nd script into a function is the execution of this 2nd script sends automated emails to a list of users. The library (SMTPlib) for some reason NEVER works inside AcMAP or PRO. It always fails. My only work around was to separate the email function into a standalone .py file outside of the tool and have it called from within the toolbox script. 

I will give the sub process module a try when I get back to work on Monday.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Try something like:

script_stdout_output = subprocess.check_output([os.path.join(sys.exec_prefix, 'python.exe'),'your_2nd_script.py','arg1', 'arg2', '...', 'argN'])
‍‍‍
Lake_Worth_BeachAdmin
Regular Contributor

returns no errors but does not execute the 2nd script... 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I agree with Luke that using subprocess is the way to go.  What if your second script does something very basic like open a file and write a line to it, does it work then?

Luke_Pinner
MVP Regular Contributor

If there's no errors then subprocess is working fine. It's your script that's the issue.  What does `script_stdout_output` contain? Do you have any error handling that hiding any errors?

Lake_Worth_BeachAdmin
Regular Contributor

Luke Pinner‌ you're correct, the subprocess module did work the issue was I was referencing a network share using \

\servername\path\to\script.py

and I forgot to include the "r" before the path.....

once changing my 2nd script path to...

r"\\servername\path\to\folder\script.py"

it works as expected. Thank you and Dan Patterson‌ for the insight on subprocess