使用Brew为Python3安装boost-python时检测到Python2.7的问题咨询
Hey there, let's break down why your brew install is still targeting Python 2.7 instead of 3.5, and how to fix this issue.
Why This Happens
When you run brew install boost-python --with-python3, Brew's default formula for boost-python still prioritizes your system's default Python (which is 2.7) to execute the ./bootstrap script. The --with-python3 flag enables Python 3 support, but it doesn't explicitly tell the bootstrap process to use Python 3 as its build interpreter. That’s why you see --with-libraries=python --with-python=python instead of python3 in the bootstrap command.
Step-by-Step Solutions
Option 1: Use Environment Variables to Force Python 3
This method explicitly tells Brew and the boost bootstrap script exactly which Python version to use:
- First, clean up any existing boost installations to avoid conflicts:
brew uninstall --force boost boost-python - Set environment variables pointing to your Python 3 installation:
export PYTHON=/usr/local/bin/python3 export PYTHON_INCLUDE=$(python3 -c "import sysconfig; print(sysconfig.get_path('include'))") export PYTHON_LIB=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") - Run the install command again with your original flags:
brew install --build-from-source --fresh -vd boost-python --with-python3
Option 2: Use Brew's Dedicated Python 3 Boost Formula
Most newer Brew versions have a separate formula for Python 3-specific boost bindings. This is the simplest and most reliable approach:
brew install boost-python3
This formula is pre-configured to build against Python 3, so you won’t need to mess with environment variables or bootstrap flags.
Option 3: Manually Adjust the Bootstrap Command (Advanced)
If the above options don’t work, you can temporarily modify Brew's boost-python formula to hardcode Python 3 in the bootstrap step:
- Edit the formula file:
brew edit boost-python - Look for the section where
./bootstrapis called. Replace any instance of--with-python=pythonwith--with-python=python3. - Save the file and run your original install command.
Note: This change will be overwritten when Brew updates the formula, so it’s only a temporary fix.
Verification
After installation, confirm the bindings are linked to Python 3 by checking the library path:
ls -la /usr/local/lib/libboost_python*.dylib
You should see files ending with 35 (matching your Python 3.5 version) instead of 27.
内容的提问来源于stack exchange,提问作者John M.

