π§ Setting Up Azure Development Tools with Python on macOS
Setting Up Azure Development Tools with Python on macOS
As a DevOps engineer working with Azure, having a properly configured local development environment is crucial. In this guide, Iβll walk you through setting up Azure tools for Python development on macOS, including some tips and tricks Iβve learned along the way.
Prerequisites
Before we begin, make sure you have:
- Python 3.8 or later
- pip (Python package manager)
- Homebrew (macOS package manager)
- A text editor (VS Code recommended)
- An Azure subscription
1. Installing Azure CLI
The Azure CLI is your command-line interface to Azure. Hereβs how to install it on macOS:
brew update && brew install azure-cli
Verify the installation:
az --version
2. Authentication Setup
There are several ways to authenticate with Azure. Here are the most common approaches:
2.1 Interactive Browser Login
az login
This opens your default browser for authentication. Itβs great for local development but not suitable for automation.
2.2 Service Principal Authentication
For automation and CI/CD, create a service principal:
az ad sp create-for-rbac --name "my-app-sp" --role contributor
Store the output securely - youβll need it for Python SDK authentication.
2.3 Environment Variables
Set up environment variables for authentication:
export AZURE_SUBSCRIPTION_ID="your-subscription-id"
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-client-id"
export AZURE_CLIENT_SECRET="your-client-secret"
Pro tip: Add these to your ~/.zshrc
or use direnv for project-specific environment variables.
3. Python SDK Setup
Install the Azure SDK for Python:
pip install azure-identity azure-mgmt-resource azure-mgmt-compute azure-mgmt-network
Hereβs a basic script to test your setup:
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
import os
# Use DefaultAzureCredential which tries different authentication methods
credential = DefaultAzureCredential()
# Create a Resource Management client
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_client = ResourceManagementClient(credential, subscription_id)
# List resource groups
for group in resource_client.resource_groups.list():
print(f"Resource Group: {group.name} - Location: {group.location}")
4. VS Code Extensions
Install these VS Code extensions for better Azure development:
- Azure Tools
- Python
- Azure Account
- Azure Resources
5. Best Practices
5.1 Using Virtual Environments
Always use virtual environments for your Python projects:
python -m venv .venv
source .venv/bin/activate
5.2 Requirements File
Keep track of your dependencies:
pip freeze > requirements.txt
Example requirements.txt
:
azure-identity>=1.12.0
azure-mgmt-resource>=21.1.0
azure-mgmt-compute>=29.1.0
azure-mgmt-network>=21.0.1
5.3 .gitignore Settings
Add these to your .gitignore
:
# Python
__pycache__/
*.py[cod]
*$py.class
.venv/
# Azure
.azure/
azureauth.json
# Environment variables
.env
.envrc
6. Troubleshooting Common Issues
6.1 Certificate Issues
If you encounter SSL certificate errors:
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/cert.pem
6.2 Token Expiration
If you get authentication errors, try:
az account get-access-token
6.3 Multiple Subscriptions
List and set the correct subscription:
az account list --output table
az account set --subscription "Your-Subscription-Name"
7. Development Workflow
Hereβs a typical workflow I use:
- Create a new project directory
- Set up a virtual environment
- Install required packages
- Configure authentication
- Create a basic script structure
Example project structure:
my-azure-project/
βββ .venv/
βββ .gitignore
βββ requirements.txt
βββ src/
β βββ __init__.py
β βββ config.py
β βββ main.py
βββ README.md
Conclusion
With this setup, youβre ready to start developing Python applications for Azure on your Mac. Remember to:
- Keep your credentials secure
- Use virtual environments
- Update your tools regularly
- Check Azure documentation for SDK updates
Resources
- Azure Python Developer Guide
- Azure SDK for Python Documentation
- Azure CLI Reference
- Authentication Best Practices
Happy coding! π