Python export from ModelBuilder Error- IndexError: list index out of range

957
2
03-30-2011 11:04 AM
CarlaYarrow
New Contributor II
Please bear with me, I'm new to using Python.

I am trying to create a scheduled task to run a model from ModelBuilder weekly.
First, I exported my model from ModelBuilder as a Python script (to verify, this models runs when manually prompted in Modelbuilder). Then, I wanted to verify the model ran as a Python script, so I ran the module using IDLE, however, I keep getting an error:

Traceback (most recent call last):
  File "\\<server>\gis\<project_folder>\Scripts\<script_name>.py", line 24, in <module>
    <event_name> = sys.argv[1]
IndexError: list index out of range

I have searched other forums with little taken from them as I cannot view line 24. I can only view the sys.argv[1] as:

<event_name> = sys.argv[1]
if <event_name> == '#':
<event_name> = "<event_name>" # provide a default value if unspecified

Please, if someone can help, offer your assistance!
0 Kudos
2 Replies
EvanThoms
Occasional Contributor II
I think you get this error because you cannot run a script from IDLE with any arguments. If you were to call this script from the command line,

eg >>>> myscript.py argument1

then argument1 gets parsed as sys.argv[1] and it would probably run, likewise from model builder.

For testing you could put in some code like this:

if not sys.argv[1]:
    event_name = raw_input('Enter an event name')


that would catch the possibility of there not being an argument and allowing you to enter it at run time
0 Kudos
curtvprice
MVP Esteemed Contributor

I know this is an old thread, but why not add this info to it:

With no argument this would still fail, as with no arguments sys.argv[1] is still out of range. Better a try except block:

try:
    event_name = sys.argv[1]
except:
    event_name = raw_input("enter an event name: ")‍‍‍‍
0 Kudos