<?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: arcpy.env.workspace = sys.path[0] gives different results in PythonWin - BUG? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469936#M36691</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I just thought I would add here that this is not a bug.&amp;nbsp; Using the sys.path will return the PYTHONPATH, which as Wayne mentioned lists the paths that Python will search for when importing modules.&amp;nbsp; In addition to the PYTHONPATH, Python will also search the PYTHONHOME directory, which is the current working directory of the script being ran.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is another way to set the workspace to the directory of the script you are running:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os, sys

arcpy.env.workspace = os.path.dirname(sys.argv[0])
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 20:48:54 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-12-11T20:48:54Z</dc:date>
    <item>
      <title>arcpy.env.workspace = sys.path[0] gives different results in PythonWin - BUG?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469934#M36689</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;When I run the following snippet in PythonWin, the results come back with the correct location (C:\PSL\Scripts).&amp;nbsp; However, when this code is loaded into the ArcMap Python Window, the results are different and incorrect!&amp;nbsp; I will be sharing a tool that will be run from the Python Window in ArcGIS and I need to create a (correct) relative workspace for it.&amp;nbsp; Suggestions?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
#import modules and define workspace
import arcpy
import os
import traceback
import sys

arcpy.env.workspace = sys.path[0]

print arcpy.env.workspace&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When run in PythonWin, I get:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; C:\PSL\Scripts&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When run in ArcMap Python Window, I get:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;C:\WINDOWS\system32\python26.zip&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need ArcMap Python Window to return the same value as the value from PythonWin!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Im using ArcGIS 10 (ArcInfo level), Python26, Windows 7&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Feb 2013 00:10:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469934#M36689</guid>
      <dc:creator>ChristiNelson1</dc:creator>
      <dc:date>2013-02-16T00:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.env.workspace = sys.path[0] gives different results in PythonWin - BUG?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469935#M36690</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sounds like you want the root where the script resides?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.workspace = sys.argv[0].rsplit(os.sep, 1)[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sys.path&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules..."&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/library/sys.html#sys.path"&gt;http://docs.python.org/2/library/sys.html#sys.path&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Based on that and similar wording for the .argv command the returned string could be 'empty' as well, meaning you do not have access to the script directory given the way you are running the script.&amp;nbsp; What you could do instead is test for an empty returned string on sys.argv[0], and instead write your results to a backup or common retrievable location.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Feb 2013 00:25:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469935#M36690</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-16T00:25:41Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.env.workspace = sys.path[0] gives different results in PythonWin - BUG?</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469936#M36691</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I just thought I would add here that this is not a bug.&amp;nbsp; Using the sys.path will return the PYTHONPATH, which as Wayne mentioned lists the paths that Python will search for when importing modules.&amp;nbsp; In addition to the PYTHONPATH, Python will also search the PYTHONHOME directory, which is the current working directory of the script being ran.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is another way to set the workspace to the directory of the script you are running:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os, sys

arcpy.env.workspace = os.path.dirname(sys.argv[0])
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:48:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-env-workspace-sys-path-0-gives-different/m-p/469936#M36691</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T20:48:54Z</dc:date>
    </item>
  </channel>
</rss>

