Install Python and pip (scripts and automation)
Set up a Python environment to run scripts and automate tasks.
What this tutorial does
The tutorial "Install Python and pip (scripts and automation)" helps you achieve this goal:
Set up a Python environment to run scripts and automate tasks.
Debian/Ubuntu
-
Install Python 3 and pip: apt install -y python3 python3-pip python3-venv -
Check: python3 --version && pip3 --version - --- Create a virtual environment (best practice) ---
-
mkdir /opt/mon-script && cd /opt/mon-script - python3 -m venv venv
- Activate environment: source venv/bin/activate
- Install packages: pip install requests flask pandas
- Disable environment: deactivate
- --- Run a script at startup (systemd) ---
-
Create /etc/systemd/system/my-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
-
Activate the service: systemctl enable --now mon-script - --- Schedule a script with cron ---
-
crontab -e then add: */5 * * * * /opt/mon-script/venv/bin/python /opt/mon-script/check.py