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?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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?
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])
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])
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.