Skip to content

Instantly share code, notes, and snippets.

@dzogrim
Created July 19, 2025 15:02
Show Gist options
  • Save dzogrim/215446f4dc0dd6bf945915f34bb84ff1 to your computer and use it in GitHub Desktop.
Save dzogrim/215446f4dc0dd6bf945915f34bb84ff1 to your computer and use it in GitHub Desktop.
This script configures system-wide defaults for Python and related tools installed via MacPorts
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Script : set-python-123.sh
# Author : Sébastien L.
#
# Description :
# This script configures system-wide defaults for Python and related tools
# installed via MacPorts. It uses `port select --set` to point commands like
# `python`, `pip`, `cython`, `ansible`, etc. to a specific installed version.
#
# By default, it targets Python 3.13 (MacPorts suffix: 313), but any other
# version suffix can be passed as an argument (e.g. `312`, `311`, etc).
#
# It supports the following tools:
# - python, python3
# - pip, pip3
# - docutils
# - cython
# - ansible
# - virtualenv
# - fonttools
#
# Usage :
# ./set-python-123.sh # Defaults to 313 ("Python 3.13")
# ./set-python-123.sh 312 # Sets to "Python 3.12" and related tools
#
# Notes :
# - Each tool has a different selector naming pattern:
# - Tools must be installed and properly registered in MacPorts selectors.
# ------------------------------------------------------------------------------
readonly NAME="$(basename "$0")"
readonly PYVERS="${1:-313}" # e.g. 313 → "Python 3.13"
readonly SET="sudo port select --set"
# Colors
cRST='\033[0m'
cGreen='\033[32;1m'
cRed='\033[31;1m'
cYellow='\033[33;1m'
cPurple='\033[35;1m'
log() { echo -e " ${cGreen}*${cRST} $*"; }
log_info() { echo -e " ${cGreen}${cRST} $*"; }
log_warn() { echo -e " ${cYellow}$*${cRST}" >&2; }
log_error() { echo -e " ${cRed}$*${cRST}" >&2; }
log_tool() { printf " %bSelecting tool:%b %-20s\n" "$cPurple" "$cRST" "$1"; }
check_tools() {
for tool in "$@"; do
command -v "$tool" >/dev/null 2>&1 || {
log_error "Missing tool: $tool"
exit 1
}
done
}
select_tool() {
local group="$1"
local version="$2"
log_tool "$group"
if port installed "$version" >/dev/null 2>&1; then
if $SET "$group" "$version" >/dev/null 2>&1; then
log_info "$version selected"
else
log_error "$version is not a valid selection"
fi
else
log_error "$version not installed"
fi
}
[[ "$(uname)" != "Darwin" ]] && log_error "This script is for macOS only." && exit 1
check_tools port sudo
log "Configuring Python ${PYVERS} tools via MacPorts"
# Standard Python/pip group
select_tool python "python${PYVERS}"
select_tool python3 "python${PYVERS}"
select_tool pip "pip${PYVERS}"
select_tool pip3 "pip${PYVERS}"
# pyXY-docutils → group = docutils, version = py313-docutils
select_tool "docutils" "py${PYVERS}-docutils"
select_tool "ansible" "py${PYVERS}-ansible"
# Special: cython group expects version = cython313
select_tool "cython" "cython${PYVERS}"
# virtualenv: version = virtualenv313
select_tool "virtualenv" "virtualenv${PYVERS}"
# fonttools: version = fonttools-313
select_tool "fonttools" "fonttools-${PYVERS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment