学完python之后就想玩些进阶的, 比如pygame. 但是在Mac OS X下安装pygame遇到了不少坑, 但最终还是顺利解决了, 也再一次深刻体会到google和百度的差距.
Installing Pygame for OS X
Note: For most of these instructions you will need to use the command line. Don’t be intimidated - programmers work on the command line all the time. Once you get used to it, you’ll find it’s the quickest and easiest way to do lots of useful things on the computer.
You access the command line by running the Terminal application - click on the Spotlight icon and type “terminal” to find it.
Step 1: Install XCode command line tools XCode is the tool from Apple for creating Mac and iOS applications. It can be installed from the App Store (it’s free). When it’s finished, type the following at the command line:
$ xcode-select --install
Step 2: Install Homebrew (http://brew.sh) Homebrew is a tool to easily install all kinds of software from the command line. It saves you having to go to a bunch of different sites and download lots of individual installers. Copy and paste this on the command line:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
and follow the directions. You’ll also need to install Homebrew Cask (http://caskroom.io):
$ brew install caskroom/cask/brew-cask
Step 3: Install the rest of the software Now we can start installing all the requirements for Pygame. Just type the following commands one at a time and let the computer do its thing:
$ brew cask install xquartz
$ brew install python3
$ brew install python
$ brew linkapps python3
$ brew linkapps python
$ brew install git
$ brew install sdl sdl_image sdl_ttf portmidi libogg libvorbis
$ brew install sdl_mixer --with-libvorbis
$ brew tap homebrew/headonly
$ brew install smpeg
$ brew install mercurial
$ pip3 install hg+http://bitbucket.org/pygame/pygame
Step 4: See if it works! Now we can see if it works. Run Python from the command line:
$ python3
and try loading Pygame:
>>> import pygame
If you don’t see an error message, you’re all set! Now head over to our YouTube Channel and start coding!
Important You will not be able to use Pygame from IDLE. You will need to run your programs from the command line like this:
$ python3 mygame.py
Some code editors will also let you run Python programs from inside the program, but we’ll talk about that in a separate post.
上面英文部分出自:KidsCanCode
有几个坑:
一开始,都是直接到pygame.org下载,熟知…上面只有32位的pygame,结果导致import pygame错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so: no matching architecture in universal wrapper
stackoverflow上面有解答:Installing Pygame for Mac OS X 10.6.8
如果你之前已经安装过homebrew,在安装sdl_image的时候遇到404,那么可能是你安装homebrew的镜像地址有问题, 你可以试试, 可能连git都装不了 方法:卸载homebrew,方法如下:
$ cd `brew --prefix`
$ rm -rf Cellar
$ brew prune
$ rm `git ls-files`
$ rm -r Library/Homebrew Library/Aliases Library/Formula Library/Contributions
$ rm -rf .git
$ rm -rf ~/Library/Caches/Homebrew
然后再按照上面的步骤重头开始吧~~~
安装完sdl之后缺link不上:
Error: The \`brew link\` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/sdl
/usr/local/bin is not writable.
You can try again using:
brew link sdl
方法:
Multiuser Homebrew
If you have multiple user accounts, and you want more than one of them to be able to use brew, you need to run through a few steps, otherwise you will constantly have to change ownership of the Homebrew file structure every time you switch users, and that’s not a great idea.
Detailed instructions can be found online, but the quick answer is this:
Create a group named brew: Open System preferences Click Accounts Click the “+” (unlock first if necessary) Under New account select Group enter brew Click Create Group Select the brew group, and add the user accounts you want to use brew to it. change the /usr/local folder group ownership:
sudo chgrp -R brew /usr/local
change the permissions to add write to /usr/local as group:
sudo chmod -R g+w /usr/local
change homebrew cache directory group:
sudo chgrp -R brew /Library/Caches/Homebrew
change the homebrew cache directory permissions:
sudo chmod -R g+w /Library/Caches/Homebrew
Single User Homebrew
If you’re not trying to use more than one user with Homebrew, then the solution provided by the other answers is probably sufficient:
sudo chown -R $(whoami) /usr/local
sudo chown -R $(whoami) /Library/Caches/Homebrew
还遇到什么坑的话,给我留言,大家一起解决~