I am learing the book: Foundations of Python Network Programming - Apress 2004

When I tried this:

import socket, sys
host = sys.argv[1]
textport =  sys.argv[2]

Then my IDE notify me this exception when debug:
"""IndexError: list index out of range:
host = sys.argv[1] """

When I tried to change number 1 and 2 above to 0 and 1 then it notify again at number 1 but not 0 anymore.:sad:

I guess that is a change in Python 2.4?!:?:
Please tell me why? Thks.

11 Years
Discussion Span
Last Post by Gorilla

Here are the basics of commandline arguments ...

# commandline arguments

import sys 

print sys.argv[0] # prints the filename of your script 
print sys.argv[1] # the first parameter
print sys.argv[2] # the second parameter
print sys.argv[1:] # all parameters

sys.arg[1] and higher is only present if you give enough arguments in your command line, for instance "myprogram.py arg1 arg2 arg3" would assign data to sys.argv[0] through sys.argv[3]. If you try to use sys.argv[4], it will give you the error you have experienced.


Thank you very much for your reply. That really helps me so much.

I now can see that when running my program from my command line, it must take into it enough params.

But when debugging using an IDE what would I do to pass those params? Or I must for get about using the sys.argv[...] command? Could you please help me with this technique?

Thank you

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.