I have a script with the following imports:
import os
import sys
import numpy as np
import netCDF4
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
sc = sys.argv[1]
run = sys.argv[2]
...
when I run it it kicks back the following error message:
Traceback (most recent call last):
File "/home/brandonsmith/climate-sensitivity-gcm/plot_scripts/map_lowcloud.py", line 11, in <module>
sc = sys.argv[1]
IndexError: list index out of range
so I checked the list contents and it came up empty:
In [28]: sys.argv
Out[28]: ['']
Why?
When you run a program from a command line prompt it receives arguments from the command line program (a shell program such as bash in *nix, the cmd prompt in Windows). An example will probably help:
> python my_program Brandon GreenMatt
In this case, you are telling the command line program to run python. The arguments you are giving to python are "my_program", "Brandon", and "GreenMatt". In turn, python tries to find and run a program named "my_program", which will receive the arguments "Brandon" and "GreenMatt".
Python's mechanism for handling arguments is sys.argv. The first argument - numbered 0, i.e. sys.argv[0] - will be the program name. Other arguments will be loaded into sys.argv in the order they appear on the command line. Thus in the example I gave above, my_program will see "Brandon" in sys.argv[1] and sys.argv[2] will hold "GreenMatt".
When there are no arguments on the command line, e.g.:
> python my_program
then sys.argv[1] and sys.argv[2] will not have anything in them, resulting in the error you see. Also, if you load the program into some sort of Python interactive environment and just run it you will see this error.
More in the docs.