Select to view content in your preferred language

Output messages from a script run within another script

1676
4
11-07-2012 01:45 PM
NathanSpears
New Contributor
I apologize for the question, which must be redundant, but the search terms for a question like this are not very specific.

I have written several python scripts and imported them in ArcCatalog into a Toolbox.  They are all working fine.

Now I would like to call several of those tools from another script to simplify a process that is repeatable.  Using ImportToolbox I have successfully done this as well, but the output messages from the scripts that are being called are not visible - only the wrapper script's messages appear.  By "output", I mean messages that I print to the ArcCatalog tool console using gp.AddMessage.

What is the best practice for this?
Tags (2)
4 Replies
MattSayler__Work_
Deactivated User
Are you talking about using functions, classes, etc from a separate script you've written?

If you put your scripts in a "known" python location (i.e. a folder in C:\Python25\Lib), it should see them like any module and you should be able to import them into a script like normal.

For example, if you have a script called MyScript.py and it's in a location python is aware of, you can bring it in with "import MyScript". You can then use functions from that script similar to how you call geoprocessing tools from arcpy: MyScript.MyFunction()

If you have a specific function you want to bring in, you can use "from MyScript import MyFunction" and then use MyFunction() directly.

I suspect (hope) that might yield better results than ImportToolbox, as you're getting out side of Esri's space and the quirks that sometimes crop up there.

Here's some more info from the python docs on using modules:
http://docs.python.org/2/tutorial/modules.html

Hope that helps!
0 Kudos
NathanSpears
New Contributor
I am familiar with modules in python but hadn't considered importing the functions directly.  I will try that out and get back to you.  Thanks for the idea!
0 Kudos
DarylVan_Dyke
Deactivated User
This may help - I sometimes run into issues depending with printing when migrating scripts between 'run in ArcGIS python console', 'run in a Toolbox tool', and 'run in a stand-alone (CMD shell or IDLE) mode'. 

This one was developed to run a log file (called fBad - because it's an error log). 

So, read in your command arguments or over-ride and explictly set the path the log file:

if arcpy.GetParameterAsText(2):
    logBadData = arcpy.GetParameterAsText(2)
else:
    logBadData = "R:\\test_pGDB"


###
#  Create Output Files
###
#output log Bad Data    
timeStamp = "2012_07_10"     # or whatever, use a function to stamp it!
fBad = open(logBadData+"_"+timeStamp+".txt",'w')


And now pass your error messages to the function

def tbxPrint(fBad,inStr):
    '''a general purpose function for try/except printing
    inside a toolbox function'''
    try:
        arcpy.AddMessage(inStr)
    except:
        print inStr
    finally:
        fBad.write(inStr+"\n")


in a manner like
tbxPrint(fBad,"\n \n \n Created "+PathAndNameGDB)
tbxPrint(fBad,"\n       with log file "+logBadData+"\n \n ")


To save 5 characters you could use default arguments if you only had 1 log file, but I didn't, so there it is.  Just remember to close your file (or file-like object) at the end of the script as good practice.

Daryl
0 Kudos
KimOllivier
Honored Contributor
You might look at the standard logging module which handles calling other modules seamlessly, but still writes to a log file.
0 Kudos