🐍
Languages & Build

Python

Install and configure Python for DevOps scripting and automation

🐧 Linux🍎 Mac🪟 Windows
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

Python is a beginner-friendly language widely used for automation, scripting, and DevOps tools.

📥

Step 01

Install Python

(01)Install Python 3

Linux
1# Ubuntu / Debian
2sudo apt-get update
3sudo apt-get install -y python3 python3-pip python3-venv python3-dev
4
5# RHEL / Fedora
6sudo dnf install -y python3 python3-pip
7
8# pyenv (multiple versions)
9curl https://pyenv.run | bash
⚙️

Step 02

Configure Python

(01)Virtual environments

Linux
1python3 -m venv ~/venvs/myproject
2source ~/venvs/myproject/bin/activate
3pip install --upgrade pip
4pip install boto3 ansible requests flask
5
6# deactivate when done
7deactivate

(02)pip and requirements.txt

Linux
1pip freeze > requirements.txt
2pip install -r requirements.txt
3
4# User-level install
5pip install --user ansible
6echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

Step 03

Verify Python

(01)Check installation

Linux
1python3 --version
2pip3 --version
3python3 -c "import sys; print(sys.executable)"

Step 04

Manage Python

(01)Common DevOps packages

Linux
1pip install ansible boto3 kubernetes docker pyyaml jinja2 requests
2pip list
3pip show boto3

📋Config templates

4 YAML templates for Python. Copy and deploy after setup.

4 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

docker-compose.yaml

PostgreSQL database + Flask app with env file and volume persistence

yaml
1version: "3.9"
2
3services:
4 db:
5 image: postgres:15-alpine
6 environment:
7 POSTGRES_DB: dukaan_db
8 POSTGRES_USER: dukaan
9 POSTGRES_PASSWORD: dukaan123
10 ports:
11 - "5432:5432"
12 volumes:
13 - pgdata:/var/lib/postgresql/data
14
15 app:
16 build: .
17 ports:
18 - "5000:5000"
19 env_file:
20 - .env
21 depends_on:
22 - db
23
24volumes:
25 pgdata:

Dockerfile

Flask application container image

dockerfile
1FROM python:3.12-slim
2WORKDIR /app
3COPY requirements.txt .
4RUN pip install --no-cache-dir -r requirements.txt
5COPY . .
6EXPOSE 5000
7CMD ["python", "app.py"]
📄

Step 01

Run with Docker Compose

(01)Build and start services

Linux
1cd your-project/
2docker compose up -d --build
3docker compose ps
4docker compose logs -f app

(02)Stop and clean up

Linux
1docker compose down
2docker compose down -v # remove volumes