DevOps
Python Scripting
Libraries & Setup

Libraries & Environment Setup

Python's power comes from its libraries. To avoid "Dependency Hell" where different projects require different versions of the same library, we use Virtual Environments.

1. Setting up your Environment

Before you start scripting, create a isolated space for your dependencies.

# Create a virtual environment named 'venv'
python3 -m venv venv
 
# Activate the environment
# On Linux/macOS:
source venv/bin/activate
 
# On Windows:
.\venv\Scripts\activate

2. Essential DevOps Libraries

These are the "must-know" libraries for any DevOps engineer.

System & Networking

  • os & sys: Basic system level interactions (file paths, environment variables).
  • subprocess: Running external shell commands from within Python.
  • paramiko: Managing SSH connections and running commands on remote servers.
  • shutil: High-level file operations (copy, move, delete directories).

Cloud & Infrastructure

  • boto3: The official AWS SDK for Python.
  • azure-sdk-for-python: Managing Microsoft Azure resources.
  • google-cloud-sdk: Interactions with Google Cloud Platform.

Web & APIs

  • requests: The golden standard for making HTTP requests (GET, POST, etc.).
  • json: Parsing and creating JSON data.
  • PyYAML: Reading and writing YAML configuration files (used everywhere in Kubernetes and Ansible).

3. Package Management

Installation is handled via pip.

# Install a library
pip install requests boto3 PyYAML
 
# Save dependencies to a file
pip freeze > requirements.txt
 
# Install from a file
pip install -r requirements.txt

[!TIP] Boto3 is King If you are working in AWS, boto3 is the most important library you will learn. It allows you to automate everything that you can do in the AWS Console.