<?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 Best practice arcpy command line and toolbox in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259404#M19946</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have some scripts I want to run both command line (schedule task) and in the desktop toolbox.&amp;nbsp; I use argparse for command line arguments but toolbox uses arcpy.getParameters...&amp;nbsp; Is there a best practice to have the same script be run in multiple ways?&amp;nbsp; I specifically don't want two scripts since the toolbox one is used to make sure the output of the scheduled one will be correct.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 05 Aug 2014 14:32:33 GMT</pubDate>
    <dc:creator>AlexanderGray</dc:creator>
    <dc:date>2014-08-05T14:32:33Z</dc:date>
    <item>
      <title>Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259404#M19946</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have some scripts I want to run both command line (schedule task) and in the desktop toolbox.&amp;nbsp; I use argparse for command line arguments but toolbox uses arcpy.getParameters...&amp;nbsp; Is there a best practice to have the same script be run in multiple ways?&amp;nbsp; I specifically don't want two scripts since the toolbox one is used to make sure the output of the scheduled one will be correct.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Aug 2014 14:32:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259404#M19946</guid>
      <dc:creator>AlexanderGray</dc:creator>
      <dc:date>2014-08-05T14:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259405#M19947</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've found that this pattern works pretty well that I use for Python toolboxes: place your code into a function with the named parameters, and then depending on how it's called, either take the command line arguments, or the parameters from ArcPy. You should be able to use this same pattern by detecting if you have any input command-line parameters (e.g. &lt;SPAN style="font-family: 'andale mono', times;"&gt;len(sys.argv)&lt;/SPAN&gt;), and if so, use those, if not, default to the getParameters arguments. Something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14072500871793229 jive_text_macro" jivemacro_uid="_14072500871793229" modifiedtitle="true"&gt;
&lt;P&gt;import sys&lt;/P&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;def main(input_fc=None, output_fc=None):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # your main script body goes here&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# executed as a script&lt;/P&gt;
&lt;P&gt;if __name__ == '__main__':&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(sys.argv) == 3:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # we were passed command line parameters, execute in a script context&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_fc=sys.argv[1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_fc=sys.argv[2]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # if we don't have args passed on the command line, assume a toolbox context&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_fc=arcpy.GetParametersAsText(0)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_fc=arcpy.GetParametersAsText(1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # call the main function with our parameters&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; main(input_fc, output_fc)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;


&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Aug 2014 14:53:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259405#M19947</guid>
      <dc:creator>ShaunWalbridge</dc:creator>
      <dc:date>2014-08-05T14:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259406#M19948</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As suggested, use sys.argv&amp;nbsp; that way there there is no script editing when you need to ascribe scripts to a tool.&amp;nbsp; The only thing to remember that sys.argv[1] is = GetParameterAsText(0) since Arcmap starts parameters at 0 and sys.argv[0] is reserved for the running script name...which can be useful&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Aug 2014 16:38:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259406#M19948</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2014-08-05T16:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259407#M19949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As an alternative you could also trap the sys.executable source to check where to expect parameters to come from.&amp;nbsp; I think it would something like this (using Shaun's code exampe):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14072627082387563" jivemacro_uid="_14072627082387563"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;import sys&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;def main(input_fc=None, output_fc=None):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # your main script body goes here&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# executed as a script&lt;/P&gt;
&lt;P&gt;if __name__ == '__main__':&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not "ArcMap" in sys.executable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # we were passed command line parameters, execute in a script context&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_fc=sys.argv[1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_fc=sys.argv[2]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # if we don't have args passed on the command line, assume a toolbox context&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_fc=arcpy.GetParametersAsText(0)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output_fc=arcpy.GetParametersAsText(1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # call the main function with our parameters&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; main(input_fc, output_fc)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;If you were to run this from an Geoprocessing Toolbox it would evaluate as 'else' and ust arcpy.GetParameter but would evaluate as "if" from another source.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edit: I don't see any option to format the code anywhere in this ridiculous updated "forum".&lt;/P&gt;&lt;P&gt;Edit 2: I had to hop over tags, blogs, rss feeds, streams, and a bunch of other things that aboslutely should not be found in a tech forum and located a "how to" on formatting code blocks.&amp;nbsp; The level of obscurity built into this "forum" is simply absurd.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Aug 2014 18:05:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259407#M19949</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-08-05T18:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259408#M19950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks James,&amp;nbsp; This solution is pretty elegant but I gave the answer to Shaun because although satisfies the initial requirements doesn't account for the possibility that the script could be called by other esri applications such as ArcCatalog, etc.&amp;nbsp; It is possible to trap for each one but then esri can come out with new applications such pro...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Aug 2014 19:00:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259408#M19950</guid>
      <dc:creator>AlexanderGray</dc:creator>
      <dc:date>2014-08-06T19:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259409#M19951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I like this solution but checking the number of paramters from arcpy would be good too since it might be invoked incorrectly from command line without the arguments, the absence of args could be either user error or toolbox...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Aug 2014 19:04:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259409#M19951</guid>
      <dc:creator>AlexanderGray</dc:creator>
      <dc:date>2014-08-06T19:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259410#M19952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I only tested from the embedded ArcCatalog (is there still a standalone version?), so you may be correct.&amp;nbsp; Although you could also just shorten the string to evaluate to just "arc" and it would pickup both "ArcMap" or "ArcCatalog" I guess.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14073565858915610" jivemacro_uid="_14073565858915610"&gt;
&lt;P&gt;&lt;SPAN class="keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="keyword"&gt;not&lt;/SPAN&gt; &lt;SPAN class="string"&gt;"Arc"&lt;/SPAN&gt; &lt;SPAN class="keyword"&gt;in&lt;/SPAN&gt; sys.executable:&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Aug 2014 20:21:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259410#M19952</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2014-08-06T20:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259411#M19953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You don't need to check environnement. Running script in toolbox or autonome script return the same result when you use GetParameterAsText&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This simple code return what you need:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;my-script.py&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

def main(a=None, b=None, c=None):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "param1: {}".format(a)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "param2: {}".format(b)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "param3: {}".format(c)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# executed as a script&amp;nbsp; 
if __name__ == '__main__':&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; a =&amp;nbsp; arcpy.GetParameterAsText(0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; b = arcpy.GetParameterAsText(1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; c = arcpy.GetParameterAsText(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # run
&amp;nbsp;&amp;nbsp;&amp;nbsp; main (a,b,c)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and call with:&lt;/P&gt;&lt;P&gt;python my-script.py "param 1" "param 2" "param 3"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:46:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259411#M19953</guid>
      <dc:creator>JeromeSeigneuret</dc:creator>
      <dc:date>2021-12-11T12:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259412#M19954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is useful Shaun, thanks. Do you have or are you aware of a location where useful arcpy patterns like this are collected? (hopefully curated?)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Aug 2017 19:21:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259412#M19954</guid>
      <dc:creator>MattWilkie3</dc:creator>
      <dc:date>2017-08-28T19:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice arcpy command line and toolbox</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259413#M19955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Matt,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't think there's any single location for tips like this. I agree it'd be nice to have, an "ArcPy patterns" resource which collected together these kinds of approaches for making Python development simpler. I'll ask around.&lt;BR /&gt;&lt;BR /&gt;Cheers, Shaun&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Sep 2017 04:09:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-arcpy-command-line-and-toolbox/m-p/259413#M19955</guid>
      <dc:creator>ShaunWalbridge</dc:creator>
      <dc:date>2017-09-15T04:09:17Z</dc:date>
    </item>
  </channel>
</rss>

