How do I use brew installed Python as the default Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
macOS ships with a system Python that should not be modified. Homebrew installs Python to /opt/homebrew/bin/ (Apple Silicon) or /usr/local/bin/ (Intel). To make the Homebrew version the default, add its directory to the front of your PATH in your shell configuration file. This ensures python3 (and optionally python) resolves to the Homebrew version instead of the system one.
Install Python via Homebrew
Homebrew installs Python to:
- Apple Silicon (M1/M2/M3):
/opt/homebrew/bin/python3.12 - Intel Mac:
/usr/local/bin/python3.12
It also creates symlinks like python3 and pip3 in the same directory.
Check Current Python Versions
Set Homebrew Python as Default
Add the Homebrew bin directory to the front of your PATH.
For zsh (default on macOS Catalina+):
For bash:
Create a python Alias (Optional)
macOS does not provide python (only python3). To use python as a command:
The alias approach is safer because it does not affect scripts that explicitly call python3.
Managing Multiple Python Versions
Homebrew supports multiple Python versions side by side:
Using pyenv (Alternative)
For frequent version switching, pyenv is more flexible than Homebrew's Python:
pyenv manages versions independently of Homebrew and supports per-project Python versions via .python-version files.
Fixing pip and Virtual Environments
Homebrew Python PATH Details
Common Pitfalls
- Modifying the system Python: macOS system Python (
/usr/bin/python3) is used by system tools. Never modify it, install packages into it globally, or delete it. Always use Homebrew Python or virtual environments for your projects. - PATH order matters: If
/usr/binappears before/opt/homebrew/binin PATH, the system Python wins. Ensure the Homebrew path is prepended (at the front), not appended. - Homebrew Python upgrades breaking virtual environments:
brew upgrade pythonmay change the Python minor version (3.11 to 3.12), breaking existing virtual environments that link to the old version. Recreate venvs after major Homebrew Python upgrades. - Using
sudo pip install: Never install Python packages withsudo. It installs into the system Python's site-packages and can break macOS tools. Usepip install --useror a virtual environment. - Confusing
pythonandpython3: macOS intentionally does not provide apythoncommand (PEP 394). Scripts using#!/usr/bin/env pythonmay fail. Usepython3explicitly or create an alias.
Summary
- Install Python with
brew install [email protected] - Add
/opt/homebrew/binto the front of your PATH in~/.zshrc - Use
python3(notpython) — create an alias if you need the shorter command - Use
pyenvfor managing multiple Python versions with per-project switching - Always use virtual environments for project dependencies
- Never modify or install packages into the macOS system Python
Related reading
- How do I use installed packages in PyCharm?
- How do I use itertools.groupby?
- How do I use method overloading in Python?
- How do I use np.newaxis?
- How do I use Pandas group-by to get the sum?
- How do I use raw_input in Python 3?
- How do I use sklearn CountVectorizer with both 'word' and 'char' analyzer? - python
- How do I use the group_by_window function in TensorFlow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.