<?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 Get script path from ToolValidator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261490#M20091</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From inside the ToolValidator in a script tool, is there a way to determine the path of the script to which a script tool is pointing?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd like to set the default value of an input data file based on its relative path to the script.&amp;nbsp;&amp;nbsp; An acceptable alternative for me would be to detect the location of the toolbox containing the script tool.&amp;nbsp; But the following return empty strings inside the ToolValidator class:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;os.getcwd
sys.argv[0]
os.path.abspath(__file__)
inspect.getfile(inspect.currentframe())&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 12 Apr 2014 13:59:35 GMT</pubDate>
    <dc:creator>LT</dc:creator>
    <dc:date>2014-04-12T13:59:35Z</dc:date>
    <item>
      <title>Get script path from ToolValidator</title>
      <link>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261490#M20091</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From inside the ToolValidator in a script tool, is there a way to determine the path of the script to which a script tool is pointing?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd like to set the default value of an input data file based on its relative path to the script.&amp;nbsp;&amp;nbsp; An acceptable alternative for me would be to detect the location of the toolbox containing the script tool.&amp;nbsp; But the following return empty strings inside the ToolValidator class:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;os.getcwd
sys.argv[0]
os.path.abspath(__file__)
inspect.getfile(inspect.currentframe())&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Apr 2014 13:59:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261490#M20091</guid>
      <dc:creator>LT</dc:creator>
      <dc:date>2014-04-12T13:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Get script path from ToolValidator</title>
      <link>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261491#M20092</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It looks like you were super close!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're looking to get the relative path to the script the Validator Tool is pointing towards, try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;file_path = os.path.dirname(os.path.abspath(__file__))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're looking to concatenate that with something else, then you could use os.path.join like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), NameOfOtherDataRelativeToYourScript)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Apr 2014 20:44:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261491#M20092</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2014-04-12T20:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: Get script path from ToolValidator</title>
      <link>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261492#M20093</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Matt!&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't know why os.path.abspath(__file__) gave me an empty string before.&amp;nbsp; (Probably the lag time between each edit of the ToolValidator is doing my head in!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In fact, it gives something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;'path to toolbox\toolboxName.tbx#scriptToolName.UpdateMessages.py'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can take it from there...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Apr 2014 21:02:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261492#M20093</guid>
      <dc:creator>LT</dc:creator>
      <dc:date>2014-04-12T21:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: Get script path from ToolValidator</title>
      <link>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261493#M20094</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Since the current working directory will be temporarily appended to your PYTHONPATH as the first or second item (depending on if it inserts a blank '' path), this also works:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import sys
cur = filter(None, sys.path)[0]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think it only inserts the '' as the first item when you are using the shell window, but using the filter *should* be fail safe.&amp;nbsp; I should also add this will only work if embedded in your script, not the shell window.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 12:49:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-script-path-from-toolvalidator/m-p/261493#M20094</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T12:49:59Z</dc:date>
    </item>
  </channel>
</rss>

