Installer Python et pip (scripts et automatisation)
Mettre en place un environnement Python pour exécuter des scripts et automatiser des tâches.
A quoi sert ce tutoriel
Le tutoriel "Installer Python et pip (scripts et automatisation)" vous aide a atteindre cet objectif:
Mettre en place un environnement Python pour exécuter des scripts et automatiser des tâches.
Debian / Ubuntu
-
Installer Python 3 et pip : apt install -y python3 python3-pip python3-venv -
Vérifier : python3 --version && pip3 --version - --- Créer un environnement virtuel (bonne pratique) ---
-
mkdir /opt/mon-script && cd /opt/mon-script - python3 -m venv venv
- Activer l'environnement : source venv/bin/activate
- Installer des paquets : pip install requests flask pandas
- Désactiver l'environnement : deactivate
- --- Exécuter un script au démarrage (systemd) ---
-
Créer /etc/systemd/system/mon-script.service : - [Unit]
- Description=Mon script Python
- After=network.target
-
[Service] - ExecStart=/opt/mon-script/venv/bin/python /opt/mon-script/app.py
- Restart=always
- User=deploy
- [Install]
- WantedBy=multi-user.target
-
Activer le service : systemctl enable --now mon-script - --- Planifier un script avec cron ---
-
crontab -e puis ajouter : */5 * * * * /opt/mon-script/venv/bin/python /opt/mon-script/check.py