Как исправить «IndexError: индекс списка вне диапазона» при запуске Systemd?Python

Программы на Python
Ответить Пред. темаСлед. тема
Гость
 Как исправить «IndexError: индекс списка вне диапазона» при запуске 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: 609ms Mar 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  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_ext Mar 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 choice Mar 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 range Mar 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 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: 545ms Mar 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  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_ext Mar 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 range Mar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE Mar 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(). Worth noting that this same script run normally without error when lunched from Geany.


Источник: https://stackoverflow.com/questions/781 ... by-systemd
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»