Senger CodeLab 🚀

How do I use brew installed Python as the default Python

September 29, 2026

📂 Categories: Python
🏷 Tags: Homebrew
How do I use brew installed Python as the default Python

Managing multiple Python installations can be a real headache, especially when you’re working with tools like Homebrew. If you’ve installed Python using brew and are struggling to make it your default version, you’re not alone. Many developers encounter this issue, juggling between system Python, brew-installed Python, and potentially even virtual environments. This guide provides a clear, step-by-step approach to setting your brew-installed Python as the default, empowering you to streamline your workflow and avoid version conflicts. We’ll explore various methods, troubleshoot common problems, and equip you with the knowledge to manage your Python installations effectively.

Understanding Python Installations

Before diving into the solutions, it’s important to grasp the different ways Python can be installed on macOS. You might have a system Python pre-installed, which Apple uses for its internal scripts. Installing Python via Homebrew places it in a separate directory, typically within /usr/local/opt/python@X.Y/bin (where X.Y represents the Python version). Recognizing these different locations is crucial for correctly configuring your default Python.

Knowing which Python versions you have and their locations is the first step. Use which python3 and which python in the terminal to identify the current default. Additionally, brew list | grep python will show your brew-installed Python versions.

Homebrew provides a handy command, brew link, to manage symbolic links. This can be used to point your system to the brew-installed Python. However, directly linking Python versions via brew link python@3.X (replace 3.X with your version) is often discouraged due to potential conflicts with the system Python. Instead, we’ll use a more robust approach.

Managing Your Python Path

The most reliable method involves manipulating your shell’s PATH environment variable. This variable dictates where your system searches for executables. By placing the path to your brew-installed Python at the beginning of the PATH, you ensure that it takes precedence. Here’s how you can achieve this:

  1. Determine your brew Python path: brew --prefix python@3.X/bin (replace 3.X with your version).
  2. Open your shell’s configuration file (e.g., ~/.bashrc, ~/.zshrc). The specific file depends on your shell.
  3. Add the following line to the end of the file, replacing the placeholder with the path from step 1: export PATH="/usr/local/opt/python@3.X/bin:$PATH".
  4. Source the updated configuration file: source ~/.bashrc (or source ~/.zshrc).

Verify the change with which python3. It should now point to the brew-installed version. This approach allows for easy switching between versions by modifying the PATH variable.

Using pyenv for Version Management

pyenv provides a more sophisticated solution for managing multiple Python installations. It allows you to easily switch between different Python versions on a per-project basis. While slightly more complex to set up, it offers unparalleled flexibility.

Install pyenv using brew: brew install pyenv. After installation, follow the post-installation instructions displayed by brew to configure your shell. Then, you can install and set specific Python versions using pyenv commands like pyenv install 3.X and pyenv global 3.X.

Troubleshooting and Common Issues

Sometimes, even after following the steps, the default Python might not switch. Here are some common issues and their solutions:

  • Incorrect Shell Configuration: Double-check that you’ve modified the correct shell configuration file and sourced it correctly.
  • Conflicting PATH Entries: Ensure no other PATH entries are overriding your brew Python path. Examine your shell configuration file for conflicting entries.

Infographic Placeholder: [Insert infographic visualizing the PATH variable and how prioritizing the brew Python path affects command execution]

Choosing the right method depends on your needs. For simpler projects, managing the PATH is often sufficient. However, for complex projects with varying Python version requirements, pyenv offers superior control and flexibility. Explore resources like the official Python documentation and Stack Overflow for in-depth guidance. Remember, choosing the right approach empowers you to maintain a clean and efficient development environment, ensuring that your projects run smoothly with the desired Python version.

  • Ensure all scripts using the !/usr/bin/env python3 shebang now use the correct Python by updating your PATH.
  • Regularly check your Python version using python3 --version to confirm you’re using the intended installation.

Learn more about managing your Python installations effectively.FAQ

Q: Why should I use brew-installed Python instead of the system Python?

A: Using brew allows you to install and manage multiple Python versions without affecting the system Python, which is essential for macOS stability. It also enables you to access the latest Python releases and libraries.

Question & Answer :
I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with

brew install python 

The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default

which python 

give me

/usr/bin/python 

and /usr/bin/python is not a symlink

How can I do to make python brew flavour to be my default python ?

As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"