URL: https://linuxfr.org/forums/linux-debian-ubuntu/posts/source-virtualenv-dans-un-script-shell Title: Source virtualenv dans un script shell Authors: electro575 Date: 2025年05月01日T10:57:59+02:00 License: CC By-SA Tags: Score: 1 Bonjour à tous, Je suis sous l'OS officiel de raspberry pi avec une pi zero W. Le paquet python3-suntime n'est pas assez récent depuis le depot http://raspbian.raspberrypi.com/raspbian/ bookworm La solution serait d'activer un virtualenv pour avoir une version plus récente du paquet suntime installé depuis pip mais, j'ai un script shell qui fait appel à un script python. Je ne sais pas comment intégrer ceci dans le script shell proprement ! ... source MyPythonProject/bin/activate Le problème se situe ici lors de l'appel d'une lib python "from astral import LocationInfo". L'erreur est la suivante et a été identifiée sous github. ```sh File "/home/jo/scripts_sun/python_script.py", line 1, in from astral import LocationInfo ImportError: cannot import name 'LocationInfo' from 'astral' (/usr/lib/python3/dist-packages/astral.py) ``` Voici le script shell principale. time_volets.sh ```sh #!/bin/bash # dès qu'une erreur est la -> on quitte le programme -> pour le dev #set -e; set -u # pour debogage #set -x # PATCH POUR CRONTAB SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # CHECK EXEC PATH ################# EXEC_PATH="$(dirname -- "${BASH_SOURCE[0]}")" # relative EXEC_PATH="$(cd -- "$EXEC_PATH" && pwd)" # absolutized and normalized if [[ -z "$EXEC_PATH" ]] ; then # error; for some reason, the path is not accessible # to the script (e.g. permissions re-evaled after suid) exit 1 # fail fi echo "$EXEC_PATH" cd /home/jo/scripts_sun ################################# # Constants / global variables ################################# # FICHIER DE LOG NOW=$(date +"%Y-%m-%d") #LOGFILE="/var/log/tracker/script_check_day.log" LOGFILE="$EXEC_PATH/log/log-$NOW.log" LOGLEVEL='DEBUG' if [ -e $LOGFILE ]; then echo "File $LOGFILE exists." else echo "File $LOGFILE does not exists." mkdir -p $EXEC_PATH/log touch $LOGFILE fi ################################# # Functions ################################# # Logging functions function log_output { echo `date "+%Y/%m/%d %H:%M:%S"`" 1ドル" echo `date "+%Y/%m/%d %H:%M:%S"`" 1ドル">> $LOGFILE } function log_debug { if [[ "$LOGLEVEL" =~ ^(DEBUG)$ ]]; then log_output "DEBUG 1ドル" fi } function log_info { if [[ "$LOGLEVEL" =~ ^(DEBUG|INFO)$ ]]; then log_output "INFO 1ドル" fi } function log_warn { if [[ "$LOGLEVEL" =~ ^(DEBUG|INFO|WARN)$ ]]; then log_output "WARN 1ドル" fi } function log_error { if [[ "$LOGLEVEL" =~ ^(DEBUG|INFO|WARN|ERROR)$ ]]; then log_output "ERROR 1ドル" fi } ############################# # "commande1 && commande2" si tu veux que la deuxième ne se lance que si la première a "réussi", "commande1 ; commande2" si tu veux que la deuxième se lance dans tous les cas function reset_relais_start_script { python relay_volet_reset_at_boot.py & wait } function command_volets_montee { python relay_volet_cuisine_montee.py & python relay_volet_milieu_salle_monte.py & python relay_volet_gauche_salle_monte.py & python relay_volet_droite_salle_monte.py & wait } function command_volets_descente { python relay_volet_cuisine_descente.py & python relay_volet_milieu_salle_descente.py & python relay_volet_gauche_salle_descente.py & python relay_volet_droite_salle_descente.py & wait } ############################# reset_relais_start_script # Executer chaque seconde le script suivant while sleep 3; do echo; date=$(date +'%d-%m-%Y %H:%M:%S') #date="10-01-2025 06:50:00" log_debug "$date" day="${date:0:2}" month="${date:3:2}" year="${date:6:4}" hour="${date:11:2}" minute="${date:14:2}" second="${date:17:2}" #echo $hour-$minute python python_script.py $year $month $day>> testpy-output.txt && echo "completed with python_script.py" . done ``` Voici le script python. ```python from astral import LocationInfo import sys #sys.argv[:1] param_1= sys.argv[1] param_2= sys.argv[2] param_3= sys.argv[3] print(param_1) print(param_2) print(param_3) #print(sys.argv) loc = LocationInfo(name='MINE', region='IDF', timezone='Europe/Paris', latitude=50.00, longitude=50.00) print(loc) print(loc.observer) #print(loc) # LocationInfo(name='SJC', region='CA, USA', timezone='America/Los_Angeles', # latitude=48.75007, longitude=-121.944675) #print(loc.observer) # Observer(latitude=37.3713439, longitude=-121.944675, elevation=0.0) import datetime from astral.sun import sun s = sun(loc.observer, date=datetime.date(2025, 5, 1), tzinfo=loc.timezone) for key in ['dawn', 'dusk', 'noon', 'sunrise', 'sunset']: print(f'{key:10s}:', s[key]) ``` Merci pour votre aide. J'ai installé un virtualenv avec les commandes suivante. ```sh sudo apt update sudo apt install python3-virtualenv virtualenv MyPythonProject source MyPythonProject/bin/activate ```

AltStyle によって変換されたページ (->オリジナル) /