Quantcast
Viewing all articles
Browse latest Browse all 14011

How To Fix "IndexError: list index out of range" when launched by Systemd?

I'm trying to fix this issue:

 crawl_oled.service - test OLED SSD1306     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)     Active: failed (Result: exit-code) since Wed 2024-03-06 08:30:14 EST; 2min 33s ago    Process: 200235 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)    Process: 200600 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)   Main PID: 200600 (code=exited, status=1/FAILURE)        CPU: 609msMar 06 08:30:14 raspberrypi python3[200600]: Traceback (most recent call last):Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>Mar 06 08:30:14 raspberrypi python3[200600]:     run_ext()Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_extMar 06 08:30:14 raspberrypi python3[200600]:     random_py = random.choice(Local_List)Mar 06 08:30:14 raspberrypi python3[200600]:   File "/usr/lib/python3.9/random.py", line 347, in choiceMar 06 08:30:14 raspberrypi python3[200600]:     return seq[self._randbelow(len(seq))]Mar 06 08:30:14 raspberrypi python3[200600]: IndexError: list index out of rangeMar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE`Mar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

...

pi@raspberrypi:~ $ cat /etc/os-release  PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" NAME="Debian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"

The script used to run normally at startup but have failed since I added the random function to randomise script launching from Luma Examples.

Here's the part from the script that is failing

def run_ext():    i = 0    while True:        try:            # Command to run "OTHER".py            ####random_file=random.choice(os.listdir())            Local_List = [each for i, each in enumerate(os.listdir(current_working_directory)) if each.endswith('1.py')]            random_py = random.choice(Local_List)            random_py = current_working_directory +'/'+ str(random_py)            print(random_py)            sp.run(random_py, timeout=10)        except sp.TimeoutExpired:            break 

When introducing I to random_py = random.choice(Local_List[i]), I get this following error:

crawl_oled.service - test OLED SSD1306     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)     Active: failed (Result: exit-code) since Wed 2024-03-06 08:52:57 EST; 2s ago    Process: 216809 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)    Process: 217172 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)   Main PID: 217172 (code=exited, status=1/FAILURE)        CPU: 545msMar 06 08:52:56 raspberrypi systemd[1]: Started test OLED SSD1306.Mar 06 08:52:57 raspberrypi python3[217172]: /Mar 06 08:52:57 raspberrypi python3[217172]: Traceback (most recent call last):Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>Mar 06 08:52:57 raspberrypi python3[217172]:     run_ext()Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_extMar 06 08:52:57 raspberrypi python3[217172]:     random_py = random.choice(Local_List[i])Mar 06 08:52:57 raspberrypi python3[217172]: IndexError: list index out of rangeMar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILUREMar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

Can someone teach me how to fix this issue and what's the best way to have this script work as expected:pick a random example from the Luma Examples folder and run it for x seconds then go on with the rest of the main()Note that this same script run normally without error when lunched from Geany.


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>