<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Output messages from a script run within another script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320421#M24889</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Are you talking about using functions, classes, etc from a separate script you've written?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you have a specific function you want to bring in, you can use "from MyScript import MyFunction" and then use MyFunction() directly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's some more info from the python docs on using modules: &lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/tutorial/modules.html"&gt;http://docs.python.org/2/tutorial/modules.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 08 Nov 2012 18:32:02 GMT</pubDate>
    <dc:creator>MattSayler__Work_</dc:creator>
    <dc:date>2012-11-08T18:32:02Z</dc:date>
    <item>
      <title>Output messages from a script run within another script</title>
      <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320420#M24888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I apologize for the question, which must be redundant, but the search terms for a question like this are not very specific.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have written several python scripts and imported them in ArcCatalog into a Toolbox.&amp;nbsp; They are all working fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I would like to call several of those tools from another script to simplify a process that is repeatable.&amp;nbsp; 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.&amp;nbsp; By "output", I mean messages that I print to the ArcCatalog tool console using gp.AddMessage.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What is the best practice for this?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Nov 2012 21:45:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320420#M24888</guid>
      <dc:creator>NathanSpears</dc:creator>
      <dc:date>2012-11-07T21:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Output messages from a script run within another script</title>
      <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320421#M24889</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Are you talking about using functions, classes, etc from a separate script you've written?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you have a specific function you want to bring in, you can use "from MyScript import MyFunction" and then use MyFunction() directly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's some more info from the python docs on using modules: &lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/tutorial/modules.html"&gt;http://docs.python.org/2/tutorial/modules.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Nov 2012 18:32:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320421#M24889</guid>
      <dc:creator>MattSayler__Work_</dc:creator>
      <dc:date>2012-11-08T18:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Output messages from a script run within another script</title>
      <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320422#M24890</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am familiar with modules in python but hadn't considered importing the functions directly.&amp;nbsp; I will try that out and get back to you.&amp;nbsp; Thanks for the idea!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Nov 2012 19:39:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320422#M24890</guid>
      <dc:creator>NathanSpears</dc:creator>
      <dc:date>2012-11-08T19:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Output messages from a script run within another script</title>
      <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320423#M24891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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'.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This one was developed to run a log file (called fBad - because it's an error log).&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, read in your command arguments or over-ride and explictly set the path the log file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if arcpy.GetParameterAsText(2):
&amp;nbsp;&amp;nbsp;&amp;nbsp; logBadData = arcpy.GetParameterAsText(2)
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; logBadData = "R:\\test_pGDB"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
###
#&amp;nbsp; Create Output Files
###
#output log Bad Data&amp;nbsp;&amp;nbsp;&amp;nbsp; 
timeStamp = "2012_07_10"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # or whatever, use a function to stamp it!
fBad = open(logBadData+"_"+timeStamp+".txt",'w')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And now pass your error messages to the function&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def tbxPrint(fBad,inStr):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''a general purpose function for try/except printing
&amp;nbsp;&amp;nbsp;&amp;nbsp; inside a toolbox function'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(inStr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print inStr
&amp;nbsp;&amp;nbsp;&amp;nbsp; finally:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fBad.write(inStr+"\n")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in a manner like&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
tbxPrint(fBad,"\n \n \n Created "+PathAndNameGDB)
tbxPrint(fBad,"\n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with log file "+logBadData+"\n \n ")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To save 5 characters you could use default arguments if you only had 1 log file, but I didn't, so there it is.&amp;nbsp; Just remember to close your file (or file-like object) at the end of the script as good practice.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Daryl&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:12:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320423#M24891</guid>
      <dc:creator>DarylVan_Dyke</dc:creator>
      <dc:date>2021-12-11T15:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Output messages from a script run within another script</title>
      <link>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320424#M24892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You might look at the standard logging module which handles calling other modules seamlessly, but still writes to a log file.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Nov 2012 07:12:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/output-messages-from-a-script-run-within-another/m-p/320424#M24892</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2012-11-13T07:12:41Z</dc:date>
    </item>
  </channel>
</rss>

