macvim/python loads the wrong Python libraries
This ticket is probably related to Homebrew/homebrew issues #26426, #17908, #20075, #20392. I tried the following with vim 7.4 (brew version) and macvim release 83.
With vim
:!which python
/usr/local/bin/python
:py import itertools
:py import os; print os.__file__
/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
With macvim
:!which python
/usr/local/bin/python
:py import itertools
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named itertools
:py import os; print os.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
Workarounds
brew unlink python but that is not what I would call a fix.
Also, when I launch macvim from a virtualenv, things work ok!
You can set pythondll like #121 and you may need to set PYTHONPATH environment variable as well.
Setting pythondll is of no help. PYTHONPATH does help, you have a point, though that should not be a solution. (virtualenv manages its shit without using PYTHONPATH and that's probably why I love it... Besides, I keep changing python/python3, brew link, brew unlink, virtualenv, etc. Hardcoding some PYTHONPATH for macvim is just a way to make me swear each time I use python3 :laughing: )
However, your comment gave me ideas to investigate more, so I had a look at the content of sys.path on a shell Python, inside vim and inside macvim.
There are 8 entries starting with /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7 that vim finds and that macvim does not.
What stays mysterious to me is how vim and macvim do not behave similarly regarding Python?
What does vim that macvim forgets to do when loading all it needs for Python?
Vim loads(dlopen) pythondll dylib or system Python2.7 dylib. That's all. You should ask at Vim mailing list or Vim github project issue If you have further question.
Could we leave the issue open so that to encourage the community (including me, esp. me!) to work for a fix? I would definitely share my work to be included if I find a proper and clean solution, and closing the issue may give the wrong signal around...
you should share it on Vim community.
Why? There is no problem with vim and nobody complains there about Python issues. The issue is on macvim which is not behaving how it should. What's wrong with leaving it the issue open and accepting solutions when they come, as long as they are decent?
Besides, asking around on vim community is not inconsistent with finding a solution to this issue yet to be solved then closed.
The issue is definitely part of Vim (--enable-python=dynamic) and you can compile Vim if_python with static link not dynamic link. Also the MacVim binary distribution can't support all Python environment.
Thank you for the extra information. Let's see where this will lead us (not today I am afraid...)
Sorry, I close this issue. The reason is that this project issues concentrate MacVim issue not Vim issue. You can still post solutions here for sharing it for MacVim community.
MacVim is linked against the system Python. Your Vim is linked against a brewed Python. I don't remember what it was, but at least at one point in time there was a reason why MacVim needs to go to the system Python. It doesn't matter what which tells you, when you're using the :py command; the :py command will use what Vim/MacVim has been built against, not what's first in your $PATH.
In fairness, @splhack, this might well be a MacVim issue; a user reports that Vim works much better than MacVim regarding Python, and I can see no reason given in this thread yet why this is "just a Vim issue".
But it also might not be a MacVim issue at all; we don't know yet. @xoolive you didn't say what flags you brewed Vim with. Also, you should try brewing MacVim with --HEAD --with-python and match as closely as you can the flags you brewed Vim with. Otherwise, it's not a fair comparison.
Thank you @chdiza. I just brew everything standard :laughing: For MacVim I would need XCode, so that's why I take the precompiled binaries (note: released two days ago) :)
I understand what you say about :py which points at the system python, so it gives consistency in the series of outputs I pasted. But then, it wouldn't explain why import itertools fail here, does it?
I'll change the default path of Python2.7 dylib, it might change the behavior. or not. If not, you need Xcode in order to build MacVim.
But then, it wouldn't explain why import itertools fail here, does it?
Maybe not, but there are lots of moving parts here besides MacVim. As you said yourself: "I keep changing python/python3, brew link, brew unlink, virtualenv, etc.". I don't see how you can do this and expect MacVim to behave consistently.
The thing to do is just install Xcode and brew MacVim from --HEAD and see what happens.
FWIW, I don't usually build with python support. But I did it just to test, and I get no error upon importing itertools. Also, MacVim found and built against a non-system python that was in my $PATH.
MacVim snapshot 83 binary works fine Homebrew python with pythondll and PYTHONPATH.
:let $PYTHONPATH="/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/"
:set pythondll="/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib"
:py import os; print os.__file__
/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
Forgot to mention, my non-system python isn't Homebrewed, and I didn't set either PYTHONPATH or &pythondll. Don't know whether those datapoints help with anything.
I am really confused now.
If I take the binary (version 83) from the release page, what is supposed to be in my sys.path? (with no $PYTHONPATH set)
I mean, from what I understand, :py calls the Python that was used to build MacVim, which would be the system Python. But then things only get properly loaded when the sys.path contains the library path for the Python that comes first in the $PATH and some libraries fail to load if they come from the system path.
What did I get wrong here?
If I take the binary (version 83) from the release page, what is supposed to be in my sys.path? (with no $PYTHONPATH set)
If you use Vanilla OS X(10.8, 10.9, 10.10, and 10.11, no additional applications, no additiona frameworks, no special environment variables), and if you use MacVim snapshot 83 binary, then Vim loads the dylib from /System/Library/Frameworks/Python.framework/Versions/2.7 and sys.path includes /System/Library/Frameworks/Python.framework/Versions/2.7.
:py can call Python 2.7 dylib, the default path is in the system path (the above path). You can set pythondll in Vim command line or .vimrc to specify the Python 2.7 dylib path. It could be your own Python 2.7 build, Homebrew, MacPorts, etc. But you may need to set $PYTHONPATH environment variable (I don't know the reason. Also I have totally no idea $PATH environment variable affects it or not)
I'll change the default path of Python2.7 dylib to the absolute path.