How to start program during boot of raspberry

January 31, 2025, 16:04

.lord.psyduck

Hi, I want to launch a program when my raspberry starts, in particular I want to tell the raspberry that before starting it must enter a virtual environment, in my case the path is /home/andrea/newenv and the python program that needs the virtual environment is /home/andrea/script.py, I personally tried to access the rc.local and put the command before exit: /bin/bash -c "source /home/pi/newenv/bin/activate but it doesn't work, does anyone know how to help me?

k9t33n

If you get the exact commands you need to do you can use crontab -e then select 1 and add at the bottom of the file @reboot [command] before selecting ctrl + o and ctrl + x

.lord.psyduck


.lord.psyduck

like this

.lord.psyduck

it doesn't work <:confusedblob:1053364681575776276>

.lord.psyduck

🥲

k9t33n

Copy and paste that command into your terminal just without the @reboot

k9t33n

Outside of crontab, I have a feeling that will give an error

.lord.psyduck

i tryed in other way

.lord.psyduck

i put the command to start the env in a python script with subprocess and i started the python script after now i see if it works

.lord.psyduck

yes it gave me an error

k9t33n

Then it's not crontabs fault

k9t33n

Crontab will simply run that command at boot

.lord.psyduck

Traceback (most recent call last): File "/home/andrea/transgoogle.py", line 1, in <module> from openai import OpenAI File "/home/andrea/newenv/lib/python3.11/site-packages/openai/__init__.py", line 8, in <module> from . import types File "/home/andrea/newenv/lib/python3.11/site-packages/openai/types/__init__.py", line 5, in <module> from .batch import Batch as Batch File "/home/andrea/newenv/lib/python3.11/site-packages/openai/types/batch.py", line 7, in <module> from .._models import BaseModel File "/home/andrea/newenv/lib/python3.11/site-packages/openai/_models.py", line 26, in <module> from ._types import ( File "/home/andrea/newenv/lib/python3.11/site-packages/openai/_types.py", line 21, in <module> import httpx File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/__init__.py", line 2, in <module> from ._api import * File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/_api.py", line 6, in <module> from ._client import Client File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/_client.py", line 13, in <module> from ._auth import Auth, BasicAuth, FunctionAuth File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/_auth.py", line 12, in <module> from ._models import Cookies, Request, Response File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/_models.py", line 48, in <module> from ._urls import URL File "/home/andrea/newenv/lib/python3.11/site-packages/httpx/_urls.py", line 6, in <module> import idna ModuleNotFoundError: No module named 'idna'

k9t33n

I can help with your actual command problems later

.lord.psyduck

ok

k9t33n

pip install idna in the env

.lord.psyduck

it says me that i have it

.lord.psyduck

i can share with you my code

.lord.psyduck

import subprocess import sys # Percorso del tuo ambiente virtuale e script venv_path = '/home/andrea/newenv/bin/activate' # Percorso dell'ambiente virtuale script_path = '/home/andrea/transgoogle.py' # Percorso del tuo script Python # Comando per attivare l'ambiente virtuale e eseguire lo script command = f"source {venv_path} && python {script_path}" # Esegui il comando subprocess.run(command, shell=True, executable="/bin/bash")

.lord.psyduck

This is the first that stars the env and starts the other script

k9t33n

Send this in with ` around it btw

k9t33n

py
import subprocess
import sys

# Percorso del tuo ambiente virtuale e script
venv_path = '/home/andrea/newenv/bin/activate'  # Percorso dell'ambiente virtuale
script_path = '/home/andrea/transgoogle.py'  # Percorso del tuo script Python

# Comando per attivare l'ambiente virtuale e eseguire lo script
command = f"source {venv_path} && python {script_path}"

# Esegui il comando
subprocess.run(command, shell=True, executable="/bin/bash")
like so, check https://discord.com/channels/818384379197784084/1189386838788542464/1193286239760158770

.lord.psyduck


.lord.psyduck

 {
                        "type": "text",
                        "text": testo_latino
                    }
                ]
            }
        ]
    )

    print("Traduzione:", completion.choices[0].message.content)
 

.lord.psyduck

and this is the other script

.lord.psyduck

oh ok, this was to long so i did a screenshot

k9t33n

Yeah Im not helping much past this tbh, i don't help much people rn cause I'm too busy

k9t33n

Unless I get some free time again, gl is all I can give ya

.lord.psyduck

ok don't worry