How to set Python's default version to 3.x on OS X?
On macOS, you may already have multiple Python versions installed (e.g., the system’s default Python 2.7 and a newer Python 3.x). If you want the command python
to run Python 3 by default, there are several approaches. Below are some commonly used methods:
1. Create an Alias
The simplest way to default to Python 3 in your personal shell sessions is to set an alias in your shell configuration file (e.g., ~/.zshrc
or ~/.bashrc
). For Zsh (the default shell on newer macOS versions):
# In your ~/.zshrc alias python="python3"
After saving the file, reload your shell:
source ~/.zshrc
Now when you type python
in a new terminal session, it will run Python 3.
Pros:
- Very simple and reversible.
- Doesn’t affect system-wide scripts.
Cons:
- Applies only to your user account and only when using a shell that reads your config file (Zsh, Bash, etc.).
2. Adjust Your PATH (Homebrew Installation)
If you install Python 3 via Homebrew, it typically places the Python 3 binary in /usr/local/bin
(for Intel Macs) or /opt/homebrew/bin
(for Apple Silicon Macs), named python3
. You can adjust your PATH
so that a symlink named python
points to the Python 3 binary.
-
Install Python 3 via Homebrew (if you haven’t already):
brew update brew install python
(This installs the latest Python 3, usually as
python3
.) -
Check where Homebrew installed Python:
which python3 # Typically /usr/local/bin/python3 or /opt/homebrew/bin/python3
-
Create a symlink named
python
(optional step if you want the commandpython
to point to Python 3 system-wide):ln -s /usr/local/bin/python3 /usr/local/bin/python
or, for Apple Silicon:
ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python
-
Ensure
/usr/local/bin
or/opt/homebrew/bin
is at the front of your PATH in your shell config file (e.g.,~/.zshrc
or~/.bashrc
):export PATH="/usr/local/bin:$PATH"
or, for Apple Silicon:
export PATH="/opt/homebrew/bin:$PATH"
Now, when you type python
in a new terminal, it should invoke Python 3.
Pros:
- Makes Python 3 available system-wide as
python
. - Straightforward for Homebrew-based workflows.
Cons:
- Changing system-wide defaults can create conflicts with scripts expecting Python 2 (though modern macOS no longer includes Python 2 by default, many older scripts still assume
python
is Python 2). - Requires you to maintain your symlinks if you install/update multiple Python versions.
3. Use pyenv
for Version Management
If you need more flexibility (e.g., different Python versions for different projects), consider using pyenv. It lets you install and switch between Python versions seamlessly.
-
Install pyenv (via Homebrew or manual setup):
brew install pyenv
-
Configure your shell (add these lines to
~/.zshrc
or~/.bashrc
):export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)"
-
Install and set a global Python 3 version:
pyenv install 3.11.2 pyenv global 3.11.2
-
Verify:
python --version # Should show Python 3.11.2 (or whichever version you installed)
Pros:
- Easily manage multiple Python versions.
- Allows per-project or global defaults.
- Minimizes the risk of breaking system scripts.
Cons:
- Slightly more involved setup.
- Must remember to use
pyenv
commands to manage versions.
Important Note About System Python
macOS used to ship with Python 2 in /usr/bin/python
and Python 3 in /usr/bin/python3
. As of macOS 12 (Monterey) and later, Apple no longer includes Python 2 by default. If you are on an older macOS version, be aware that replacing or altering the system Python can break system scripts or tools that rely on Python 2. It’s best practice to leave Apple’s Python alone and install your own version(s) of Python in /usr/local/
or /opt/homebrew/
, then adjust your environment accordingly.
Level Up Your Python Skills
Once you’ve set up Python 3 as your default, you may want to enhance your Python knowledge further. Consider these resources from DesignGurus.io:
-
Grokking Python Fundamentals
A thorough introduction to Python concepts, from syntax basics to data structures, perfect for ensuring you have a solid foundation. -
Grokking the Coding Interview: Patterns for Coding Questions
Ideal if you’re preparing for coding interviews and want to tackle algorithmic challenges with confidence.
And if you’re aiming to land a role at a major tech company, you’ll often need system design expertise. For that:
- Grokking System Design Fundamentals
A comprehensive guide to scaling and architecting systems, crucial for senior-level interviews.
Don’t forget the DesignGurus YouTube Channel for free video resources on both coding and system design topics.
Final Thoughts
Switching your default Python on macOS to Python 3 can be done in multiple ways, from adding a simple alias to using dedicated tools like pyenv
. The best approach depends on how deeply you want to customize your environment, whether you’re managing multiple Python versions, and how comfortable you are with system-level changes. In any case, be cautious with the system Python that ships with macOS—avoid overwriting it and rely on user-level or Homebrew installations to keep everything working smoothly.
Happy Coding!