You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

macOS环境下Brew提示已安装最新Python3.9,但python3命令仍指向旧版本的问题求助

Troubleshooting Python Version Mismatch on macOS

Hey there! Let's figure out why your installed Python 3.9 isn't being recognized even after running those Homebrew commands. This is a common issue caused by PATH environment variable priority—here's how to fix it:

Step 1: Identify the Current Python Path

First, check where your python3 command is pointing right now:

which python3

If this returns /usr/bin/python3, that's Apple's pre-installed Python 3.6, which is why you're seeing that version instead of Homebrew's 3.9.

Next, find where Homebrew installed Python 3.9:

brew --prefix python@3.9
  • For Intel Macs, this will likely be /usr/local/opt/python@3.9
  • For Apple Silicon (M1/M2) Macs, it'll be /opt/homebrew/opt/python@3.9

The actual executable lives in the bin subfolder of that path (e.g., /usr/local/bin/python3 or /opt/homebrew/bin/python3).

Step 2: Adjust Your PATH Environment Variable

The problem is that your system is checking /usr/bin (where Apple's Python lives) before Homebrew's bin directory. We need to flip that order.

Edit Your Shell Configuration File

Depending on which shell you use:

  • If you're using Zsh (default on macOS Catalina and later), edit ~/.zshrc
  • If you're using Bash, edit ~/.bash_profile or ~/.bashrc

Open the file with a text editor (e.g., nano ~/.zshrc), and add this line at the top:

# For Intel Macs
export PATH="/usr/local/bin:$PATH"

# For Apple Silicon Macs
# export PATH="/opt/homebrew/bin:$PATH"

Uncomment the line that matches your Mac type, then save and exit (for nano: Ctrl+O, Enter, Ctrl+X).

Apply the Changes

Reload your shell configuration to make the changes take effect immediately:

# For Zsh
source ~/.zshrc

# For Bash
source ~/.bash_profile

Step 3: Verify the Fix

Now check again which Python is being used:

which python3

It should now point to Homebrew's bin directory (e.g., /usr/local/bin/python3). Then confirm the version:

python3 -V

You should see Python 3.9.x displayed!

Bonus Check: If You Use pyenv (Optional)

If you have pyenv installed for Python version management, it might be overriding your Homebrew setup. In that case, you can switch to the Homebrew-installed version with:

pyenv global 3.9.1

Give these steps a try—this should resolve the version mismatch issue!

内容的提问来源于stack exchange,提问作者John

火山引擎 最新活动