You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS下将python2.7链接至brew安装的最新2.7.13版本

Fixing Python 2.7 Path to Point to Brew's 2.7.13 on macOS

Hey there, let's tackle this issue step by step— I've run into similar problems with brew-managed Python versions on macOS before, so I know exactly where to look. The core issue here is either your system's PATH prioritizing the old Python version, missing symlinks, or scripts hardcoding the system Python path. Let's break down the fixes:

1. Check Your Current Python 2.7 Path First

First, confirm where your shell is pulling python2.7 from. Run this command in your terminal:

which python2.7

If the output is /usr/bin/python2.7 (the system's old 2.7.5 version), that's the root of the problem—your PATH is prioritizing the system directory over brew's.

2. Adjust Your PATH to Prioritize Brew's Python

Brew installs Python 2.7.13 at /usr/local/Cellar/python/2.7.13/bin. We need to make sure this directory comes first in your PATH.

Temporary Test (for current session only)

Run this to update your PATH temporarily:

export PATH="/usr/local/Cellar/python/2.7.13/bin:$PATH"

Now run which python2.7 again—you should see the brew path. If this works, let's make it permanent.

Permanent PATH Update

Depending on your shell (Bash or Zsh), edit the corresponding config file:

  • For Bash: Open ~/.bash_profile in your editor and add the line above.
  • For Zsh: Open ~/.zshrc instead.

After adding the line, save the file and reload the config:

# For Bash
source ~/.bash_profile

# For Zsh
source ~/.zshrc

Sometimes brew doesn't create a python2.7 symlink in /usr/local/bin (which is usually in your PATH before /usr/bin). Fix this by creating the symlink manually:

ln -s /usr/local/Cellar/python/2.7.13/bin/python2.7 /usr/local/bin/python2.7

Verify it worked with which python2.7—it should now point to /usr/local/bin/python2.7, which links to your brew version.

4. Fix Scripts That Hardcode the System Python

Many scripts use a hardcoded shebang line like #!/usr/bin/python2.7, which bypasses your PATH entirely and calls the system version. To fix this:

Update Individual Scripts

Change the shebang line at the top of each script from:

#!/usr/bin/python2.7

To:

#!/usr/bin/env python2.7

This tells the script to use the python2.7 found in your PATH.

Batch Update Scripts (If You Have Many)

Use this command to batch-update all .py files in your current directory (and subdirectories):

find . -name "*.py" -exec sed -i '' 's|#!/usr/bin/python2.7|#!/usr/bin/env python2.7|' {} \;

(Note: The empty '' after sed -i is required for macOS's version of sed.)

5. Verify the Fix

Restart your terminal (to ensure all config changes take effect), then run:

python2.7 --version

You should see Python 2.7.13 printed. To confirm scripts are using the right version, add this line to the top of a script (after the shebang) and run it:

import sys
print(sys.executable)

It should output /usr/local/Cellar/python/2.7.13/bin/python2.7.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:47:20