安装kivy

1.下载对应mac下的官方压缩包https://kivy.org/#download
2.解压出来后是Kivy2.app或Kivy3.app(python2对应Kivy2,python3对应Kivy3)
3.将解压出来的文件复制到mac应用程序文件夹下

sudo mv Kivy2.app /Applications/Kivy.app

4.创建符号链接,以便在终端直接执行kivy命令即可启动加载了kivy的python解释器

ln -s /Applications/Kivy.app/Contents/Resources/script /usr/local/bin/kivy

注意系统环境变量应包含/usr/local/bin路径,解释器为/Applications/Kivy.app/Contents/Resources/venv/bin/python

pycharm配置

方案一、后续会出错

  • 安装完成kivy后进入目录
    cd /Applications/Kivy.app/Contents/Resources/kivy
  • 执行如下命令
    USE_OSX_FRAMEWORKS=0 kivy setup.py install
    其中USE_OSX_FRAMEWORKS=0等同于注释掉setup.py中的c_options['use_osx_frameworks'] = platform == 'darwin'语句,并添加c_options['use_osx_frameworks'] = False语句;执行kivy setup.py install给kivy自带的python解释器加载kivy包,从而不用通过scripy脚本来启动解释器。
  • 在pycharm进行如下设置
    找到Preference->Project Interpreter->Add Local,选择/Applications/Kivy.app/Contents/Resources/python
  • 确认import kivy不会报错即可

方案二、最终正确方案

  • 注意:前面安装的kivy是一个官方打包好的Python虚拟环境,当在终端执行kivy时,实际上是运行kivy.app内部的Python解释器,下面安装的kivy只是将其加载为anaconda中Python解释器的一个模块
  • 依赖安装
    官网下载SDL2,SDL2_image,SDL2_mixer,SDL2_ttf最新版本的dmg安装文件,将相应的framework复制到资源库中的Frameworks文件夹,官方下载gstreamer及其devel版的最新pkg安装包,两个都要安装

  • 检查import pygame是否出现如下错误

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/__init__.py", line 133, in <module>
    from pygame.base import *
    ImportError: dlopen(/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so, 2): Library not loaded: /usr/local/opt/sdl/lib/libSDL-1.2.0.dylib
    Referenced from: /Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so
    Reason: image not found
    # 若出现错误,则根据提示安装相应的包,上述错误提示为加载libSDL-1.2.0.dylib,下载SDL-1.2.x包,复制framework
  • 下载源码文件
    git clone http://github.com/kivy/kivy
    cd kivy
    sudo make
    sudo python setup.py install
    # 前提是已将conda的python解释器设置为系统默认的python解释器

遇到错误的分析及解决

  • sudo brew install sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer出现如下错误:

    ==> Downloading http://ftp.gnome.org/pub/gnome/sources/glib/2.42/glib-2.42.1.tar.xz
    Already downloaded: /Library/Caches/Homebrew/glib-2.42.1.tar.xz
    ==> Downloading https://gist.githubusercontent.com/jacknagel/af332f42fae80c570a77/raw/7b5fd0d2e6554e9b770729fddacaa2d64832
    curl: (22) The requested URL returned error: 404 Not Found
    Error: Failed to download resource "glib--patch"
    Download failed: https://gist.githubusercontent.com/jacknagel/af332f42fae80c570a77/raw/7b5fd0d2e6554e9b770729fddacaa2d648327644/glib-hardcoded-paths.diff

    解决方法:更新brew

  • 更新出现brew upgrade出现如下错误

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- utils/popen (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/Library/Homebrew/utils.rb:6:in `<top (required)>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/Library/Homebrew/global.rb:9:in `<top (required)>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/local/Library/brew.rb:16:in `<main>'

    分析解决:
    The problem mainly occurs after updating OS X to El Capitan (OS X 10.11). This is mainly because of file permission issues with file El Capitan’s new SIP process. Try changing permissions of /usr/local directory.

    # sudo chown -R $(whoami):admin /usr/local
    # 上面无效,故使用下面语句更新brew版本至最新
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • USE_OSX_FRAMEWORKS=0 pip install kivy出现如下错误

    creating /System/Library/Frameworks/Python.framework/Versions/2.7/share
     error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted

    分析解决:
    权限问题,USE_OSX_FRAMEWORKS=0 pip install --user kivy

  • import pygame出现如下错误:
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/__init__.py", line 133, in <module>
    from pygame.base import *
    ImportError: dlopen(/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so, 2): Library not loaded: /usr/local/opt/sdl/lib/libSDL-1.2.0.dylib
    Referenced from: /Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so
    Reason: image not found

    分析解决:
    缺少SDL-1.X.X,官方网站下载最新的SDL-1.2.X,将frameworks复杂到资源库中的Frameworks文件夹

It is hard to get pip working on El Capitan for several reasons:

  • OS X doesn't set some distutils variables correctly, so pip tries to install ancillary files in locations under /System/Library/. El Capitan blocks this, which is the error you are running into.
  • OS X includes a number of outdated packages under /System/Library/. pip often wants to upgrade these but cannot on El Capitan.
  • OS X places /System/Library/ higher in the python search order than /Library/Python/2.7/site-packages (the system-wide python package location), so even if you manage to install newer versions of some packages, the old ones still get loaded, breaking some dependencies.
最后由 oucb 编辑于2016年09月24日 15:45