Updating Python on Mac
Categories:
Mastering Python Updates on macOS: A Comprehensive Guide

Learn the safest and most effective methods to update Python on your macOS system, avoiding common pitfalls and ensuring a stable development environment.
Updating Python on macOS can be a nuanced task, often leading to confusion due to pre-installed system Python versions, third-party package managers, and various installation methods. This guide aims to demystify the process, providing clear, step-by-step instructions to ensure you can update Python safely and efficiently, maintaining a robust development setup.
Understanding Python on macOS
Before diving into updates, it's crucial to understand how Python typically resides on a macOS system. macOS comes with a system-provided Python (usually an older 2.x or 3.x version) which is essential for many internal system utilities. Never attempt to modify or remove this system Python. Doing so can lead to system instability. Instead, we'll focus on installing and managing separate Python versions for your development needs.
flowchart TD A[Start: Check Current Python Versions] --> B{System Python Present?} B -->|Yes| C[DO NOT TOUCH System Python] B -->|No| D[Proceed with Installation/Update] C --> E[Choose Installation Method] D --> E E --> F{Homebrew?} F -->|Yes| G[Use Homebrew for Python Management] F -->|No| H{pyenv?} H -->|Yes| I[Use pyenv for Multiple Versions] H -->|No| J[Direct Installer from python.org] G --> K[Update/Install via Homebrew] I --> L[Install/Switch via pyenv] J --> M[Run Installer] K --> N[Verify Installation] L --> N M --> N N --> O[End: Python Updated/Installed]
Decision flow for updating Python on macOS
Method 1: Using Homebrew (Recommended)
Homebrew is the most popular package manager for macOS and the recommended way to install and manage Python versions for development. It keeps your development Python separate from the system Python and makes updates straightforward.
1. Step 1: Install Homebrew (if not already installed)
Open your Terminal application and run the following command. Follow the on-screen instructions to complete the installation.
2. Step 2: Install or Upgrade Python 3.x
If you already have Python 3 installed via Homebrew, this command will upgrade it to the latest stable version. If not, it will install it. Homebrew handles linking the new version and ensuring it's in your PATH.
3. Step 3: Verify the Installation
After installation or upgrade, verify that the correct Python version is being used. Homebrew typically symlinks its Python to /usr/local/bin/python3
.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Homebrew
brew update
brew upgrade python
# Or, if installing for the first time:
brew install python
Update or install Python 3.x using Homebrew
python3 --version
which python3
Verify Python version and path
brew update
before brew upgrade python
to ensure Homebrew's package definitions are up-to-date. This helps prevent issues with outdated dependencies.Method 2: Using pyenv for Multiple Python Versions
For developers who need to switch between multiple Python versions (e.g., 3.8, 3.9, 3.10) for different projects, pyenv
is an excellent tool. It allows you to install and manage multiple Python versions and easily switch between them globally, locally (per project), or within your shell session.
1. Step 1: Install pyenv via Homebrew
First, ensure Homebrew is installed (see Method 1, Step 1). Then, install pyenv
.
2. Step 2: Configure your Shell for pyenv
Add pyenv
initialization to your shell's configuration file (.zshrc
for Zsh, .bash_profile
or .bashrc
for Bash). This ensures pyenv
commands are available and Python versions are managed correctly.
3. Step 3: Install a Specific Python Version
Use pyenv install
to download and compile a specific Python version. You can list available versions with pyenv install --list
.
4. Step 4: Set Global or Local Python Version
Set a global Python version for your entire system or a local version for a specific project directory. Local settings override global settings.
5. Step 5: Verify the Active Python Version
Confirm that pyenv
is managing your Python version correctly.
brew install pyenv
Install pyenv using Homebrew
# For Zsh (default on modern macOS):
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
# For Bash:
echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
Configure your shell for pyenv
pyenv install 3.10.12
pyenv install 3.11.5
Install specific Python versions
# Set global version
pyenv global 3.11.5
# Or set local version in a project directory
mkdir my_project && cd my_project
pyenv local 3.10.12
Set global or local Python versions
python --version
which python
pyenv versions
Verify active Python version and installed pyenv versions
pyenv
, ensure you restart your terminal or source
your shell configuration file after making changes to the pyenv
setup to apply them.Method 3: Direct Installer from python.org
While Homebrew and pyenv
are generally preferred, you can also download and run the official macOS installer from python.org. This method is straightforward but offers less flexibility for managing multiple versions or system-wide updates compared to Homebrew.
1. Step 1: Download the Installer
Visit the official Python website (python.org) and navigate to the downloads section for macOS. Download the latest stable Python 3.x installer (.pkg file).
2. Step 2: Run the Installer
Locate the downloaded .pkg
file and double-click it. Follow the on-screen instructions. The installer will typically place Python in /Library/Frameworks/Python.framework/Versions/X.Y
and create symlinks in /usr/local/bin
.
3. Step 3: Verify Installation
Open your Terminal and check the installed Python version.
python3 --version
which python3
Verify Python version and path after using the official installer
PATH
environment variable if the new Python version isn't picked up automatically. The installer usually handles this, but it's good to be aware.