Skip to content

Instantly share code, notes, and snippets.

@RhetTbull
Last active October 9, 2023 16:46
Show Gist options
  • Save RhetTbull/1455dffe2343913adc5e3693dd2ff433 to your computer and use it in GitHub Desktop.
Save RhetTbull/1455dffe2343913adc5e3693dd2ff433 to your computer and use it in GitHub Desktop.
Switch between multiple python versions on macOS when using python.org official installer
#!/bin/bash
# Change symlink to Python version in /usr/local/bin/python to point to a
# specified version as installed by the official python.org installer for macOS
# usage: ./pyver.sh <version>
# when called without arguments, prints the currently linked Python version
# This is useful for use with pyenv (https://github.com/pyenv/pyenv) which can only
# handle a single "system" python version and offers no way to switch between multiple
# system pythons
# WARNING: use with caution as this changes the system python path which could break scripts / apps
# expecting a specific version
if [ "$#" -ne 1 ]; then
readlink /usr/local/bin/python
exit 1
fi
PY_PATH="/Library/Frameworks/Python.framework/Versions/$1/bin/python3"
if [ -f "$PY_PATH" ]; then
echo "Python $1 exists, changing the link..."
ln -sf "$PY_PATH" /usr/local/bin/python
echo "Link changed successfully. New Python version:"
readlink /usr/local/bin/python
else
echo "Python version $1 is not installed or the path is incorrect."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment