Question

Run command: Python Script fails due to collectstatic

I have set up a Django website with Github following the tutorial. I have also set up an associated job, which runs a python script before every deploy. The python script fails with the following error:

elif len(sys.argv) > 0 and sys.argv[1] != 'collectstatic':
IndexError: list index out of range

I assume this is because my python script calls django.setup(), which then triggers the code above from the settings.py file. As there are no other argv in the run command the index 1 results in out of range (i.e. the run script is simply: python script.py, so no arguments)

How do I fix this? And in general, what’s the best way to run a python script that connects to my Django website?

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @2e35cd875dd3-4a53-9f58-21496b,

You’ll get the Indexerror: list index out of range error when you try and access an iaem using a value that is out of the index range of the list and does not exist. This is quite common when you try to access the last item of a list, or the first one if you’re using negative indexing

The best way to check this is with a for cycle. Here is an example:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

for name in range(5):
    print(names[name])

Hi @2e35cd875dd3-4a53-9f58-21496b,

You’ll get the Indexerror: list index out of range error when you try and access an item using a value that is out of the index range of the list and does not exist. This is quite common when you try to access the last item of a list, or the first one if you’re using negative indexing

The best way to check this is with a for cycle. Here is an example:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

for name in range(5):
    print(names[name])

Okay, so I fixed this JUST after posting, by simply running --noinput to the run command. I will let the question stay up, in case anyone else comes across the issue :-)

Maybe someone has a good explanation as to why this worked/was necessary?