Skip to content

Python 2.x and Python 3.x

Kazuki Sakamoto edited this page Jul 17, 2016 · 12 revisions

MacVim binary distribution is enabled to use Python 2.7 and Python 3.5 out of the box with Homebrew Pythons (These Python 2.x and 3.x version number may vary with MacVim snapshot version. Please take a look at release description.)

.vimrc example for pyenv install 2.7.11

let $PYTHONHOME=$HOME."/.pyenv/versions/2.7.11"
set pythondll=$HOME/.pyenv/versions/2.7.11/lib/libpython2.7.dylib

and for pyenv install 3.5.1

let $PYTHONHOME=$HOME."/.pyenv/versions/3.5.1"
set pythonthreedll=$HOME/.pyenv/versions/3.5.1/lib/libpython3.5m.dylib

But MacVim binary distribution doesn't allow to use Python 2.7 and Python 3.5 at the same time due to Python shared modules.

E837: This Vim cannot execute :py3 after using :python
E263: Sorry, this command is disabled, the Python library could not be loaded.

You must build MacVim, Python 2.x and 3.x as the following way by yourself if you really really want to use Python 2.x and 3.x at the same time.

  • Install python 2.7.11

Use pyenv.

$ PYTHON_CONFIGURE_OPTS="--enable-shared" \
    LDSHARED="clang -bundle" \
    LDCXXSHARED="clang++ -bundle" \
    BLDSHARED="clang -bundle -lpython2.7" \
    pyenv install 2.7.11
  • Verify python 2.7.11

Use otool to verify the binary.

$ otool -L $HOME/.pyenv/versions/2.7.11/lib/python2.7/lib-dynload/_ctypes.so
/Users/foo/.pyenv/versions/2.7.11/lib/python2.7/lib-dynload/_ctypes.so:
    /Users/foo/.pyenv/versions/2.7.11/lib/libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

libpython2.7.dylib should be included in the result.

  • Install python 3.5.1

Use pyenv.

$ PYTHON_CONFIGURE_OPTS="--enable-shared" \
    LDSHARED="clang -bundle" \
    LDCXXSHARED="clang++ -bundle" \
    BLDSHARED="clang -bundle -lpython3.5m" \
    pyenv install 3.5.1
  • Verify python 3.5.1

Use otool to verify the binary.

$ otool -L $HOME/.pyenv/versions/3.5.1/lib/python3.5/lib-dynload/_ctypes.cpython-35m-darwin.so
/Users/foo/.pyenv/versions/3.5.1/lib/python3.5/lib-dynload/_ctypes.cpython-35m-darwin.so:
        /Users/foo/.pyenv/versions/3.5.1/lib/libpython3.5m.dylib (compatibility version 3.5.0, current version 3.5.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

libpython3.5m.dylib should be included in the result.

  • Install MacVim with the option

Use the official MacVim Homebrew formula with the option.

$ brew tap macvim-dev/macvim
$ brew install --HEAD macvim-dev/macvim/macvim --with-properly-linked-python2-python3
  • Verify

Check version output

$ /usr/local/Cellar/macvim/HEAD/bin/vim --version|grep python
+cscope          +lispindent      +python/dyn      +viminfo
+cursorbind      +listcmds        +python3/dyn     +vreplace

Both +python/dyn and +python3/dyn should be here.

  • Sample setting

.vimrc

let $PYTHONHOME=$HOME."/.pyenv/versions/2.7.11"
set pythondll=$HOME/.pyenv/versions/2.7.11/lib/libpython2.7.dylib
py import sys
let $PYTHONHOME=$HOME."/.pyenv/versions/3.5.1"
set pythonthreedll=$HOME/.pyenv/versions/3.5.1/lib/libpython3.5m.dylib
py3 import sys
  • Test

Execute this vim script

function! s:py3_test()
    py3 import time
    py3 from ctypes import *
    py3 lib = cdll.LoadLibrary("/usr/lib/libc.dylib")
    py3 print(time.ctime(lib.time(0)))
endfunction
function! s:py_test()
    py import time
    py from ctypes import *
    py lib = cdll.LoadLibrary("/usr/lib/libc.dylib")
    py print(time.ctime(lib.time(0)))
endfunction
call s:py3_test()
call s:py_test()
Clone this wiki locally