Skip to content

Python 2.x and Python 3.x

Kazuki Sakamoto edited this page Dec 4, 2016 · 12 revisions

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

But MacVim binary distribution doesn't allow to use Python 2.7 and Python 3.5.2 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. It's not common use case.

  • Install python 2.7.11

Use pyenv. It requires in order to use Python 2.x and 3.x interface in MacVim, it doesn't mean that you have to use this pyenv for you Python runtime.

$ 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.2

Use pyenv. It requires in order to use Python 2.x and 3.x interface in MacVim, it doesn't mean that you have to use this pyenv for you Python runtime.

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

Use otool to verify the binary.

$ otool -L $HOME/.pyenv/versions/3.5.2/lib/python3.5/lib-dynload/_ctypes.cpython-35m-darwin.so
/Users/foo/.pyenv/versions/3.5.2/lib/python3.5/lib-dynload/_ctypes.cpython-35m-darwin.so:
        /Users/foo/.pyenv/versions/3.5.2/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

This setting is just sample, and it just uses the pyenv Python runtime what we used to build MacVim. The path is vary with your system.

.vimrc

set pythonhome=$HOME/.pyenv/versions/2.7.11
set pythondll=$HOME/.pyenv/versions/2.7.11/lib/libpython2.7.dylib
set pythonthreehome=$HOME/.pyenv/versions/3.5.2
set pythonthreedll=$HOME/.pyenv/versions/3.5.2/lib/libpython3.5m.dylib
  • 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