Making MacVim play nice with IPython (and friends)

Posted by

I have had a devil of a time replicating my laptop setup on my desktop.

The most frustrating issue has been a weird import error thrown when MacVim attempts to connect to IPython (the error is much broader than this, but that was the context in which the error appeared for me).


Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 44, in km_from_string
  File "/usr/local/lib/python2.7/site-packages/IPython/__init__.py", line 43, in
 <module>
    from .config.loader import Config
  File "/usr/local/lib/python2.7/site-packages/IPython/config/__init__.py", line
 16, in <module>
    from .application import *
  File "/usr/local/lib/python2.7/site-packages/IPython/config/application.py", l
ine 31, in <module>
    from IPython.config.configurable import SingletonConfigurable
  File "/usr/local/lib/python2.7/site-packages/IPython/config/configurable.py",
line 26, in <module>
    from loader import Config
  File "/usr/local/lib/python2.7/site-packages/IPython/config/loader.py", line 2
7, in <module>
    from IPython.utils.path import filefind, get_ipython_dir
  File "/usr/local/lib/python2.7/site-packages/IPython/utils/path.py", line 19,
in <module>
    import tempfile
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/
lib/python2.7/tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/
lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

After a little digging, it became clear that the issue was a more basic one, with the brew installed macvim itself.  The way you can tell is by trying the following:

1/ open macvim and enter the following “:py import os; print os.__file__” (for those that have not used vim before, you first press colon — Control-; on my keyboard — and then type in “py import os; print os.__file__).

Image

After you hit enter, you should see the location of your python os module.  If this worked, you’d think importing all the other stuff would too, right?

Image

Well, you’d be wrong to think that.

2/ try “:py from random import Random”. If you got the first error, you will almost certainly get a shorter version of the error i printed in full at the top. The bottom line being:

ImportError: ...

After much frustration and digging about i found this blog post.  It seems that the problem is with the MacVim build.  You have to build it with the -v –force tags.

The full solution:

$ brew unlink python
$ brew unlink macvim
$ brew remove macvim
$ brew install -v --force macvim
$ brew link macvim
$ brew link python

So now, when you “:py from random import Random” nothing will happen; and when you execute “:IPython” in MacVim (assuming you have vim-ipython installed), you ought to find that MacVim connects to IPython without fuss.

One comment

Comments are closed.