Hemant Pandey
Hemant Pandey

Reputation: 173

Error in Python Script regarding str(sys.argv[1])

import MySQLdb as mdb
import math
import sys 
from decimal import Decimal

conn=mdb.connect('localhost','root','','testdb')
c=conn.cursor()

user_id=str(sys.argv[1])
lati=str(sys.argv[2])
longi=str(sys.argv[3])
radius=int(str(sys.argv[4]))

#user_id=1
#lati='28.635308'
#longi='77.224960'
#radius=1.5

The error is :

Traceback (most recent call last):
  File "C:\Python27\recommend.py", line 9, in <module>
    user_id=str(sys.argv[1])
IndexError: list index out of range

However the dummy values which is commented by # are working fine. Any help in resolving this error ?

Upvotes: 1

Views: 885

Answers (1)

molcay
molcay

Reputation: 86

One way to checking the mistake you can print out the sys.argv list and check the number of elements it has. The output of the error was saying there is only one element (most probably it's the script's itself -script name-) in the sys.argv. Most probably when you are running the script you don't supply the arguments(you must supply 4 arguments).

Upvotes: 2

Related Questions