Skip to content

Instantly share code, notes, and snippets.

@chriswayg
Last active July 23, 2020 06:12
Show Gist options
  • Save chriswayg/ee97606a4dc93a4cdacff90915d5d1e5 to your computer and use it in GitHub Desktop.
Save chriswayg/ee97606a4dc93a4cdacff90915d5d1e5 to your computer and use it in GitHub Desktop.
Find and list most installed Python versions on a macOS system
#!/bin/bash
set -o errexit
# pythonlister version 0.3
# License: MIT - (c) 2020 ChrisWayg
# Run the script directly
# /bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/chriswayg/ee97606a4dc93a4cdacff90915d5d1e5/raw/pythonlister.sh)"
# Download, install with sudo in /usr/local/ and run the script
# f=/usr/local/bin/pythonlister; sudo mkdir -p /usr/local/bin && sudo curl -sSLo $f https://gist.githubusercontent.com/chriswayg/ee97606a4dc93a4cdacff90915d5d1e5/raw/pythonlister.sh && sudo chmod +x $f && pythonlister
# make sure you understand the script, before you install it with sudo.
color="$(tput bold; tput setaf 5)"
reset="$(tput sgr0)"
#clear
printf "============== LIST INSTALLED PYTHON VERSIONS ON THE SYSTEM ===============\n"
#printf "The script will display 'python --version' for the Python executables\n"
#printf "found in the file-system and in the PATH\n"
# Python.org - https://www.python.org/downloads/
# Miniconda https://docs.conda.io/en/latest/miniconda.html
# Homebrew & Pyenv https://brew.sh/
# Pyenv https://github.com/pyenv/pyenv
# MacPorts https://ports.macports.org/
findPython=("2.3" "2.4" "2.5" "2.6" "2.7" "3.4" "3.5" "3.6" "3.7" "3.8" "3.9" "3.10" "3.11" "3.12")
#printf "\n*** Searching for Python " && echo ${findPython[@]}
checkPaths=("/System/Library/Frameworks/Python.framework/Versions" \
"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions" \
"/Library/Frameworks/Python.framework/Versions" \
"$HOME/Library/Frameworks/Python.framework/Versions" \
"/usr/local/Cellar/python*/*/Frameworks/Python.framework/Versions" \
"/Users/$(id -un)/.pyenv/versions")
#echo "Check paths: ${checkPaths[@]}"
# the array expansion is not double-quoted to allow filename expansion
for searchPath in ${checkPaths[@]}; do
case $searchPath in
/System/Library/Frameworks/Python.framework/*)
installedBy="macOS in System"
;;
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/*)
installedBy="Xcode CLT in Library"
;;
*/Library/Frameworks/Python.framework/*)
installedBy="Python.org in Library"
;;
/usr/local/Cellar/*)
installedBy="Homebrew in cellar"
;;
/Users/*/.pyenv/*)
installedBy="Pyenv for user"
;;
*)
installedBy="Unknown"
;;
esac
[[ -d $searchPath/ ]] && printf "${color}*** Installed by $installedBy: ${reset}" && ls $searchPath/
if [[ -d $searchPath/ ]]; then
#echo "- search path $searchPath"
for ver in "${findPython[@]}"; do
#echo "- looking for python${ver}"
path_of_python=$searchPath/${ver}*/bin/python${ver}
if command -v $path_of_python &> /dev/null; then
printf "==> $path_of_python - " && $path_of_python -V 2>&1 | head -n 4
fi
done
fi
done
checkPaths=("/opt/local/bin" \
"/Users/$(id -un)/.pyenv/shims" \
"/Users/$(id -un)/miniconda*/bin" \
"/Users/$(id -un)/anaconda*/bin")
# the array expansion is not double-quoted to allow filename expansion
for searchPath in ${checkPaths[@]}; do
case $searchPath in
/opt/local/*)
installedBy="Macports in opt"
;;
/Users/*/.pyenv/*)
installedBy="Pyenv in shims"
;;
/Users/*/miniconda*)
installedBy="Miniconda for user"
;;
/Users/*/anaconda*)
installedBy="Anaconda for user"
;;
*)
installedBy="Unknown"
;;
esac
[[ -d $searchPath/ ]] && printf "${color}*** Installed by $installedBy: ${reset}\n"
if [[ -d $searchPath/ ]]; then
#echo "- search path $searchPath"
for ver in "${findPython[@]}"; do
path_of_python=$searchPath/python${ver}
#echo "- looking in $path_of_python"
if command -v $path_of_python &> /dev/null; then
printf "${color}==> $path_of_python - ${reset}" && $path_of_python -V 2>&1 | head -n 4
fi
done
fi
done
printf "\n============== Listing currently activated Python versions ===============\n"
findPython=("Python" "2" "2.3" "2.4" "2.5" "2.6" "2.7" "3" "3.4" "3.5" "3.6" "3.7" "3.8" "3.9" "3.10" "3.11" "3.12")
#printf "\n*** Searching for versions of " && echo ${findPython[@]}
printf "${color}*** In PATH:${reset} $PATH\n"
for ver in "${findPython[@]}"; do
[[ $ver == "Python" ]] && ver=""
pythonver="python${ver}"
#echo "- looking for $pythonver"
if command -v $pythonver &> /dev/null; then
printf "${color}The command '$pythonver' will run ${reset}" && which $pythonver
printf "==> Version: " && $pythonver -V 2>&1 | head -n 4
fi
done
if command -v pip -V &> /dev/null; then
printf "${color}The command 'pip' will run ${reset}" && which pip
printf "==> Version: " && pip -V 2>&1 | head -n 4
fi
if command -v pip3 -V &> /dev/null; then
printf "${color}The command 'pip3' will run ${reset}" && which pip3
printf "==> Version: " && pip3 -V 2>&1 | head -n 4
fi
if command -v pipenv --version &> /dev/null; then
printf "${color}The command 'pipenv' will run ${reset}" && which pipenv
printf "==> Version: " && pipenv --version 2>&1 | head -n 4
fi
printf "\n"
[[ ! -z $VIRTUAL_ENV ]] || [[ ! -z $CONDA_DEFAULT_ENV ]] && printf "${color}==> NOTE:${reset} you are running in a virtual environment! \n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment