Mac OS 10.13(High Sierra)下导入pygame模块失败求助
Fixing Pygame Installation Issues on macOS (Python 2.7/3.x)
Hey there, let's break down what's going wrong and how to fix it step by step—this Pygame installation mess on macOS is super common, especially with older Python versions like 2.7, which is already end-of-life.
First, Let's Understand Your Errors
- The
no matching architecture in universal wrappererror happens because the Pygame build you downloaded is 32-bit only, but the Python 2.7 you installed runs as 64-bit. macOS can't mix architectures for modules, so they won't work together. - The 32-bit Python 2.7 IDLE failing to open is likely because modern macOS versions (even Intel-based ones) prioritize 64-bit applications, and older 32-bit frameworks are no longer supported.
Option 1: Ditch Python 2.7 (Recommended)
Python 2 has been unsupported since 2020, and Al Sweigart's books (like Making Games with Python & Pygame) have updated versions that work with Python 3. This will save you a ton of headaches:
- Uninstall any existing Python 2.7 setups (homebrew, framework installs, etc.) to avoid conflicts.
- Install a modern Python 3 version:
- Download from the official Python website, or use homebrew with
brew install python3.
- Download from the official Python website, or use homebrew with
- Install Pygame for Python 3:
- Run this command in your terminal:
pip3 install pygame
- Run this command in your terminal:
- Test it out:
- Open Python 3's IDLE or run
python3in terminal, then typeimport pygame. If no errors pop up, you're good to go!
- Open Python 3's IDLE or run
Option 2: Fix Python 2.7 + 32-bit Pygame (Not Recommended, But Possible)
If you absolutely need to stick with Python 2.7 for your book's exercises, use pyenv to manage a 32-bit Python 2.7 installation (this avoids system-level conflicts):
- Install homebrew if you haven't already, then install pyenv:
brew install pyenv
- Configure your shell to use pyenv (follow the instructions it outputs after installation).
- Install a 32-bit version of Python 2.7.18 (the last Python 2 release):
env PYTHON_CONFIGURE_OPTS="--enable-framework --with-universal-archs=32-bit" pyenv install 2.7.18
- Set this version as your default (or use it for your project folder):
pyenv global 2.7.18(global) orpyenv local 2.7.18(project-specific)
- Install Pygame for this 32-bit Python:
- Use the
pygame-1.9.2pre-py2.7-macosx10.7file you downloaded earlier, install it withpip install pygame-1.9.2pre-py2.7-macosx10.7.pkg
- Use the
- Test the import again in Python 2.7—this should resolve the architecture mismatch error.
Why Your Previous Attempts Failed
- Resetting your system didn't fix it because the core issue was architecture compatibility, not a corrupted install.
- The homebrew-installed Python 2.7 is 64-bit by default, which clashed with the 32-bit Pygame build you downloaded.
- Standalone 32-bit Python 2.7 frameworks don't play nice with modern macOS's security and architecture requirements, hence the IDLE failure.
内容的提问来源于stack exchange,提问作者Michael Carlos
相关产品推荐
相关产品推荐

