sys.argv[1] IndexError: list index out of range in Python

avatar

Borislav Hadzhiev

Last updated: Apr 22, 2022

banner

Photo from Unsplash

sys.argv[1] IndexError: list index out of range in Python #

The sys.argv "IndexError: list index out of range in Python" occurs when we run a Python script without specifying values for the required command line arguments. To solve the error, provide values for the required arguments, e.g. python main.py first second.

sys argv indexerror list index out of range

Here is an example of how the error occurs.

main.py
import sys print(sys.argv) # 👉️ ['main.py'] print(sys.argv[0]) # 👉️ 'main.py' # ⛔️ IndexError: list index out of range print(sys.argv[1])

I ran the script with python main.py.

The sys.argv list contains the command line arguments that were passed to the Python script.

Where arv[0] is the name of the script, argv[1] is the first provided command line argument, etc.

To solve the error, make sure to provide all of the required command line arguments when running the script, e.g. python main.py first second.

main.py
import sys print(sys.argv) # 👉️ ['main.py', 'first', 'second'] print(sys.argv[0]) # 👉️ 'main.py' print(sys.argv[1]) # 👉️ 'first' print(sys.argv[2]) # 👉️ 'second'

Notice that the first item in the list is always the name of the script.

It is operating system dependent if this is the full pathname or not.

If you don't have to always specify all of the command line arguments that your script tries to access, use an if statement to check if the sys.argv list contains the index that you are trying to access.

main.py
import sys print(sys.argv) # 👉️ ['main.py'] idx = 1 if len(sys.argv) > idx: print(sys.argv[idx]) else: # 👇️ this runs print(f'index {idx} out of range')

I ran the script as python main.py without providing any command line arguments, so the condition wasn't met and the else block ran.

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If a list has a length of 1, then its last index is 0 (because indexes are zero-based).

This means that you can check if the list's length is greater than the index you are trying to access.

Alternatively, you can use a try/except block to handle the error.

main.py
import sys print(sys.argv) # 👉️ ['main.py'] try: print(sys.argv[1]) except IndexError: # 👇️ this runs print('index out of range')

We tried accessing the list item at index 1 which raised an IndexError exception.

You can handle the error or use the pass keyword in the except block.

Conclusion #

The sys.argv "IndexError: list index out of range in Python" occurs when we run a Python script without specifying values for the required command line arguments. To solve the error, provide values for the required arguments, e.g. python main.py first second.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.