前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mac OS X 10.11 安装 Pygame

Mac OS X 10.11 安装 Pygame

作者头像
Locker
发布2018-08-01 17:36:56
1.2K0
发布2018-08-01 17:36:56
举报
文章被收录于专栏:221-B221-B

学完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:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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):

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ python3

and try loading Pygame:

代码语言:javascript
复制
>>> 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:

代码语言:javascript
复制
$ 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错误:

代码语言:javascript
复制
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,方法如下:

代码语言:javascript
复制
$ 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不上:

代码语言:javascript
复制
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:

代码语言:javascript
复制
sudo chgrp -R brew /usr/local

change the permissions to add write to /usr/local as group:

代码语言:javascript
复制
sudo chmod -R g+w /usr/local

change homebrew cache directory group:

代码语言:javascript
复制
sudo chgrp -R brew /Library/Caches/Homebrew

change the homebrew cache directory permissions:

代码语言:javascript
复制
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:

代码语言:javascript
复制
sudo chown -R $(whoami) /usr/local

sudo chown -R $(whoami) /Library/Caches/Homebrew

还遇到什么坑的话,给我留言,大家一起解决~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档