Wednesday, February 11, 2009

Compiling Vim (on Windows) To Use Different Python Versions

I recently wanted to use Python Omni Complete for Vim, but whenever I executed the complete command <c-x><c-o> it failed with the error:
Error: Required vim compiled with +python
E117: Unknown function: pythoncomplete#Complete
This is incorrect. After doing a :version command, Vim showed it was compiled for Python support (the +python/dyn option was present). The real problem was Vim could not find the correct version of Python it was expecting: I had 2.6 installed, Vim wanted 2.4 (DYNAMIC_PYTHON_DLL="python24.dll").

Following is one way to compile Vim to use a different version of Python than that shipped with Vim. I targeted 2.6, but the steps can be tweaked to work for any version of Python. It is assumed Vim and Python are already installed.
  1. Obtain a C++ compiler. I considered two, GCC from Cygwin and GCC from MinGW. I went with the latter simply because it was easy to install only the C/C++ development tools (e.g. GCC, make) that I needed. The Cygwin installer had far too many packages to chose from. I didn't want to waste my time determining what was necessary to install from packages I had no interest in.
  2. Obtain Vim source code and unzip somewhere, say C:\.
  3. Edit the MinGW make file. For me that is C:\vim\vim72\src\Make_ming.mak. Find the variable PYTHON_VER and set the version number to whatever version of Python you have installed. Also, find the variable PYTHON and set to the root of the Python installation directory. In my case,
    PYTHON=c:/Programs/Python26
    PYTHON_VER=26
    
  4. MinGW did not add the location of the GNU make tool to my PATH, so I had to manually do that next. Since I had no intentions of using this tool after I was done compiling Vim, I updated my path temporarily. To do that, I opened a command prompt in the directory of the make file (C:\vim\vim72\src) and executed
    set PATH=%PATH%;C:\Programs\MinGW\bin
    
    where C:\Programs\MinGW is the location where I installed MinGW.
  5. Compile GVim. Using the same command prompt window in which PATH was changed, execute
    mingw32-make -f Make_ming.mak gvim.exe
    
    Normally you would execute make -f Make_ming.mak gvim.exe, but make.exe seems to have been renamed to mingw32-make.exe in the MinGW installation.
  6. Install gvim.exe. After compiling is done, locate gvim.exe in the same directory as the make file (C:\vim\vim72\src) and copy to Vim's runtime directory. For my, C:\Program Files\Vim\vim72. There should already be a gvim.exe there.
  7. Make sure the Python 2.6 installation is on your path, namely the directory where Python26.dll is located.