0

I'm using Vim compiled with both +python/dyn and +python3/dyn1. I don't use Python2 anymore and never want Vim to use Python2, but it seems to find Python2 before Python3 and uses it instead.

I've read elsewhere that if at the top of my .vimrc I include:

if has('python3')
endif

then Vim will use Python3. Unfortunately, this has not been my experience. When I include that at the top of my .vimrc, Vim still finds Python2 first and thus ignores Python3.


1 I use the pre-compiled MacVim for both the gui and the command-line.

5
  • Do you perhaps have the symlink /usr/bin/python set to python2? Commented Sep 9, 2021 at 13:02
  • 1
    it seems to find Python2 before Python3 What does this exactly mean? Finds who and when? Commented Sep 9, 2021 at 13:11
  • 1
    For the record, it's substantially better to leave /usr/bin/python pointing at Python 2 if you have it installed. There's an uncomfortable amount of legacy programs that use #!/usr/bin/python and who mean Python 2. Commented Sep 9, 2021 at 14:03
  • 1
    That said, what exactly are you asking? 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 Commented Sep 9, 2021 at 14:05
  • Python 2 and 3 have worked side by side for me on Vim 8.0-8.2, so whatever "finding" you think is taking place, it's most likely not ignoring Python 3. Again, what context is this taking place in? Commented Sep 9, 2021 at 14:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Answer is now less messy.
So it seems that if I want to force Python3, then set pyxversion=3 seems like the right thing to do. When I do that and test with :py3 print() I get an error message that indicates other things are not set up quite right on my machine. I'll have to investigate what is going on.
You may have to point Vim to the right dynamic libraries. If so, see this gist for a MacPorts-based setup that should be relatively easy to adapt to your circumstances.
0

Vim sources python one of 2 ways afaik: -- Precompiled [vim --version will return "+python" -- Dynamically [vim --version will return "+python/dyn"

You are obviously in the second category; for this you need to do these in order

  1. Confirm the version compatibility 32-bit Vim --> 32 bit Python and likewise for 64-bit Vim
  2. On windows ensure that the pythonthreedll directive points to your source for dynamic load

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.