The problem
Vim's problem with the Python 2.x and 3.x interfaces is that it can only use one or the other:
- the first use of
:python prevents further use of :python3,
- and the first use of
:python3 prevents further use of :python.
The hack
has('python3') is supposed to load the 3.x interface as a side effect so the point of the hack is, if done very early in the initialisation process, to make sure the 3.x interface is loaded first. It is essentially the same as doing something like :py3 ...: all it does is make :py unusable.
MacVim is special
MacVim is not built with the python or python3 features so has('python') and has('python3') will always return 0.
Instead, you have python_dynamic and python3_dynamic, which can be tested the same way:
if has('python3_dynamic')
endif
But there are a few issues with that:
has('python3_dynamic') doesn't have the same side effect has has('python3') so the hack won't work,
has('python3_dynamic') and has('python_dynamic') will always be true because they test for the features, which are always there.
Reality check
In any case, the :python family of commands always uses Python 2.x so, if you are trying to use Python 3.x with :py you can drop the towel right here and right now as Python 3.x is only usable via the :python3 and :pythonx families of commands.
Hacks and compromises
If all you want is to make sure that :py3 and friends are usable, one approach would be to do something like:
py3 print()
near the top of your vimrc, which will load the 3.x interface early, and thus prevent further loading of the 2.x interface, and thus break :py and friends.
Another approach would be to use the :pythonx family of commands with the 3.x interface:
set pyxversion=3
For more info, see :help python-2-and-3.
/usr/bin/pythonset topython2?it seems to find Python2 before Python3What does this exactly mean? Finds who and when?/usr/bin/pythonpointing at Python 2 if you have it installed. There's an uncomfortable amount of legacy programs that use#!/usr/bin/pythonand who mean Python 2.if has("python3")is COMPLETELY unrelated to which Python is used, and doesn't affect commands. It's a check for whether or not Python 3 is supported and linked in your current environment. That's why it doesn't do anything - whatever source made that claim is blatantly wrong. "Vim still finds Python2 first and thus ignores Python3" - finds how? In what context does Vim prefer Python 2 to Python 3? Vim never ignores Python 3 if it finds Python 2, unless MacVim does something obscure literally no other implementation of Vim does