<?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 Error when importing python script (IndexError: list index out of range) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/error-when-importing-python-script-indexerror-list/m-p/102849#M7925</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I'm converting some of my pre-existing python scripts (that worked in version 9.3.1) to work with version 10 (and python 2.6). I have a script that has 3 arguments and a function. It works fine in 9.3.1 but in 10.0 (python 2.6) it is giving me an error when I simply try to import the script ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; from ExportIndividualFeatures2_v10 import ExpIndFeatures&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "&amp;lt;interactive input&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "ExportIndividualFeatures2_v10.py", line 23, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inWorkspace = sys.argv[1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;IndexError: list index out of range&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, this error occurs when trying to simply import the script at the command line in PythonWin (see the command issued after the "&amp;gt;&amp;gt;&amp;gt;" above)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The relevant parts of the script I am importing are these lines at the beginning ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, string, os, arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;inWorkspace = sys.argv[1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inShapeFile = sys.argv[2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inField = sys.argv[3]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def ExpIndFeatures(inWorkspace, inShapeFile, inField):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc. etc. (code in here)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Why does this work with 9.3.1 (python 2.52 pywin 32 build 210) and not with 10.0 (python 2.6 pywin 32 build 212) ???&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 29 Aug 2012 12:51:36 GMT</pubDate>
    <dc:creator>JohnChurchill</dc:creator>
    <dc:date>2012-08-29T12:51:36Z</dc:date>
    <item>
      <title>Error when importing python script (IndexError: list index out of range)</title>
      <link>https://community.esri.com/t5/python-questions/error-when-importing-python-script-indexerror-list/m-p/102849#M7925</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I'm converting some of my pre-existing python scripts (that worked in version 9.3.1) to work with version 10 (and python 2.6). I have a script that has 3 arguments and a function. It works fine in 9.3.1 but in 10.0 (python 2.6) it is giving me an error when I simply try to import the script ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; from ExportIndividualFeatures2_v10 import ExpIndFeatures&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "&amp;lt;interactive input&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "ExportIndividualFeatures2_v10.py", line 23, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inWorkspace = sys.argv[1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;IndexError: list index out of range&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, this error occurs when trying to simply import the script at the command line in PythonWin (see the command issued after the "&amp;gt;&amp;gt;&amp;gt;" above)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The relevant parts of the script I am importing are these lines at the beginning ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, string, os, arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;inWorkspace = sys.argv[1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inShapeFile = sys.argv[2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inField = sys.argv[3]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def ExpIndFeatures(inWorkspace, inShapeFile, inField):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc. etc. (code in here)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Why does this work with 9.3.1 (python 2.52 pywin 32 build 210) and not with 10.0 (python 2.6 pywin 32 build 212) ???&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 12:51:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-importing-python-script-indexerror-list/m-p/102849#M7925</guid>
      <dc:creator>JohnChurchill</dc:creator>
      <dc:date>2012-08-29T12:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: Error when importing python script (IndexError: list index out of range)</title>
      <link>https://community.esri.com/t5/python-questions/error-when-importing-python-script-indexerror-list/m-p/102850#M7926</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When you import a python module, the interpreter executes any code in the outer scope (code not inside functions or not protected like the example below.&amp;nbsp; This is true for 2.7 and was true in 2.5.&amp;nbsp; So when you get to the sys.argv calls, an exception occurs, since anything past sys.argv[0] is passed by a command line.&amp;nbsp; What you need to do is put your code in function(s) and then slice sys.argv&amp;nbsp; below an if __name__&amp;nbsp; == '__main__' line, like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

import sys, string, os, arcpy

def ExpIndFeatures(inWorkspace, inShapeFile, inField):
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc. etc. (code in here)

if __name__ == '__main__':

&amp;nbsp;&amp;nbsp;&amp;nbsp; inWorkspace = sys.argv[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; inShapeFile = sys.argv[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; inField = sys.argv[3]

&amp;nbsp;&amp;nbsp;&amp;nbsp; result = ExpIndFeatures(inWorkspace, inShapeFile, inField)


&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What the if __name__ == '__main__' part does is keep any code it below from executing unless the module is run as a script.&amp;nbsp; Do this, and you will be able to import your module without an exception, and you can then call ExpIndFeature with args.&amp;nbsp; There are other ways of doing this, but the example above is a standard python idiom.&amp;nbsp; And it's practical, since it makes your module dual-purpose as either an importable module or an executable script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good luck&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:20:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-importing-python-script-indexerror-list/m-p/102850#M7926</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2021-12-11T06:20:14Z</dc:date>
    </item>
  </channel>
</rss>

