37

In the google searches I've done, the command I should be using is brew install vim --with-python3, which succeeds without error, but then vim --version reports Python 3 as unavailable.

k162:~ asday$ brew install vim --with-python3
==> Using the sandbox
==> Downloading https://github.com/vim/vim/archive/v7.4.2210.tar.gz
Already downloaded: /Users/asday/Library/Caches/Homebrew/vim-7.4.2210.tar.gz
==> ./configure --prefix=/usr/local --mandir=/usr/local/Cellar/vim/7.4.2210/share/man --enable-multibyte --with-tlib=ncurses --enable-cscope --with-compiledby=Homebrew --enable-p
==> make
==> make install prefix=/usr/local/Cellar/vim/7.4.2210 STRIP=true
🍺  /usr/local/Cellar/vim/7.4.2210: 1,732 files, 28.5M, built in 52 seconds
k162:~ asday$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 27 2015 16:22:14)
Compiled by [email protected]
Normal version without GUI.  Features included (+) or not (-):
-arabic +autocmd -balloon_eval -browse +builtin_terms +byte_offset +cindent
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
-conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs
-dnd -ebcdic -emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path
+find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv
+insert_expand +jumplist -keymap -langmap +libcall +linebreak +lispindent
+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape
-mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm -mouse_sysmouse
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype
+path_extra -perl +persistent_undo +postscript +printer -profile +python/dyn
-python3 +quickfix +reltime -rightleft +ruby/dyn +scrollbind +signs
+smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
 -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
 -xterm_clipboard -xterm_save
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe
Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses

And doing :py3 print("") within vim returns E319: Sorry, the command is not available in this version.

What is it I'm doing wrong? Do I need to compile vim myself?

4
  • 1
    Did you try brew install vim --without-python --with-python3 (that is, explicitly specify --without-python as well). Aug 19, 2016 at 6:54
  • 1
    Yes, but that didn't work. I needed to uninstall vim completely and do brew cleanup, then brew install vim --with-python3 worked. Aug 21, 2016 at 0:05
  • Have you by any chance figured the solution, @AdamBarnes? For some reason, I'm right now failing to build Homebrew's vim8 --with-python3...
    – A S
    Nov 8, 2016 at 20:23
  • I'd try brew remove vim then brew cleanup, then try the command listed in my comment from 21/08. If that doesn't work then I have no idea; I'm fairly convinced mine working now is a fluke - hence not posting my comment as an answer. Nov 9, 2016 at 10:56

13 Answers 13

26

The following works as of 26 May 2017 when a -python3 version of Vim is already installed via homebrew:

brew remove vim
brew cleanup
brew install vim --with-python3
4
  • 8
    this now reports Warning: vim: this formula has no --with-python3 option so it will be ignored!
    – Tommy
    May 11, 2018 at 16:15
  • 14
    @Tommy, that's because Python3 is now the default python in Homebrew. If you run brew info vim, you'll see that there's an option (--with-python@2) to use Python2 instead of Python3.
    – Michael
    May 11, 2018 at 21:13
  • @Tommy May I further infer that you are looking at this question today because Homebrew recently installed or upgraded python@2, perhaps as a dependency, and it's confounding your Vim scripts?
    – Michael
    May 11, 2018 at 21:16
  • seeing some of the output (like brew info vim) following this install would be useful
    – baxx
    Mar 13, 2020 at 14:47
10

Already get to make it work. vim formula installs vim with python3 support only if python --version is version 3. Doesn't matter if brew install python install python version 3 (because it does in another binary: python3)

From: https://www.reddit.com/r/vim/comments/8iymh8/vim_in_osx_with_python3_support/dyx1b94/

Here are my steps:

  1. alias python=python3
  2. brew install vim
  3. at this point, I get +python3 in the vim --version output
  4. unalias python
4
  • It seems to me that you’re reporting that somebody else got it to succeed.  Do you have first-hand experience with this?  Can you explain what Adam needs to do to get it to work on his system? Oct 30, 2018 at 17:33
  • @Scott thanks for the comment. Yes, I tried this before posting the answer. Here are my steps: 1. alias python=python3 2. brew install vim 3. at this point, I get +python3 in the vim --version output 4. unalias python
    – Ali Cirik
    Oct 30, 2018 at 20:03
  • Thanks for responding.  Please edit your answer to include the relevant information.   (You’ll be able to format it better there.) Oct 31, 2018 at 1:43
  • I was able to get rid of python (2) support in MacVim using this method. It was causing the dreaded this vim cannot execute py3 after using python issue. Thanks! Jul 22, 2020 at 13:57
6

Since version 1.6.0 of Homebrew (2018-04-09), the default python version is 3. Thus, if you have a version of vim installed before this change which is compiled with python2, what you need to do now to get vim with python3 is simply:

$ vim --version | grep python
$ brew uninstall --force vim
$ brew cleanup
$ brew install vim
$ vim --version | grep python
2
  • what's the output of vim --version | grep python that we should be expecting here?
    – baxx
    Mar 13, 2020 at 13:24
  • @baxx just to verify that the version is Python 2 before the upgrade, and Python 3 after upgrade
    – Erikw
    Feb 9, 2022 at 12:18
4

It is because there was an already installed vim in your system which is normally found in /usr/loca/vim

$ which vim

You can try ls /usr/local/Cellar/vim/7.4.2210 and take a look what folders are available. In my system, in that directory i have bin/vim, so I update my .zshrc or similar rc file in your os, update it as

alias 'vim'=/usr/local/Cellar/vim/7.4.2210/bin/vim

When you open a new terminal, then

which vim

It will show the update path. So you can use the python3 inside it.

2
  • I added the alias to my ~/.bash_aliases instead. I also used /usr/local/bin/vim as the alias which is created by homebrew via brew link vim May 9, 2020 at 6:41
  • I installed vim using brew and alias it as said in this answer. But my vim was located in /opt/homebrew/Cellar/vim/9.0.0200/bin/vim Aug 24, 2022 at 5:27
4

I got the same issue today. No matter

brew install vim --with-python3

or

brew install vim --without-python --with-python3

So i check Homebrew's logs at ~/Library/Logs/Homebrew/vim, in 01.configure i found

checking Python is 3.0 or better... too old

This is because configure can not find python3's path

By adding /usr/local/Cellar/python3/3.X.X/bin (your path may be different) to your PATH envoriment and re-run, the vim finally got compiled with +python3

1
  • 1
    It seems that install vim with brew and python 3 support also requires python to be install with brew and set in the path.
    – swade
    Mar 16, 2018 at 17:25
4

Put the following in you .zshrc or anything . rc:

if (command -v brew && brew list --formula | grep -c vim ) > /dev/null 2>&1; then
    alias vim="$(brew --prefix vim)/bin/vim"
fi

This will resolve to Homebrew's vim only when homebrew is installed`

2
  • This works for me. I just install macvim and start vim from $(brew --prefix vim)/bin/bim. Thanks. Sep 26, 2021 at 5:51
  • @VincentHuang it's a bit slower to run brew --prefix vim everytime you run Vim because it needs to load brew first. The advantage of making it an alias is that the brew prefix is fetched only once, when defining the alias, and then it's just a fixed path. I'm happy that this helped you ! :D Dec 7, 2021 at 12:11
3

Another way to install Vim with Python3 support is using port from The MacPorts Project:

To check how many variants available for VIM and their conflict:

port variant vim

To install vim with python3 support:

sudo port install vim +python37

And to uninstall, just:

sudo port uninstall vim
1
  • Thanks, for me --with-python3 wasn't working with brew but using ports made the trick. Apr 2, 2019 at 13:31
1

In latest brew, --with-python3 has been deprecated,here

try this,

brew remove vim
brew cleanup
brew install vim -vd protobuf

restart the terminal, and do

vim --version
0

As of October 2017 this works.

If you have previously installed vim with homebrew, uninstall it first

brew remove vim
brew cleanup

Then

brew install vim --without-python --with-python3
1
  • 5
    vim: this formula has no --with-python3
    – swade
    Mar 16, 2018 at 17:06
0

It's possible that vim is still resolving to the Apple-installed version. See: https://stackoverflow.com/questions/33313708/apples-vim-always-used-instead-of-homebrew

1
  • 1
    You can try echo $EDITOR to see if it's set to vi (pointing to Apple's vim) or vim, which is presumably your own.
    – Michael
    Aug 30, 2018 at 18:45
0

This variation turned out to be my problem:

I was activating a Python VirtualEnv automatically in .bash_profile/.zshrc before adding certain things (such as homebrew) to my path.

The solution was to put the source venv/bin/activate line at the end of .bash_profile, and then run deactivate when I launched my terminal to install vim. At that point, I followed the above answers and ran:

brew remove vim; brew cleanup; brew install vim

and everything was good.

0

Posting in case any fellow tmux users run into this issue. tmux was using the right shell (zsh in my case), but because my .zshrc file didn't specify the vim installation to use, it defaulted to the system one instead of the new version I installed with python3 support (using brew install vim -vd protobuf iirc). I noticed this by running which vim both inside and outside tmux, which returned different paths. The resolution was to add an alias to my .zshrc file to specify the vim version I wanted to use.

0

If you don't mind using neovim, then install as you'd normally do:

brew install nvim

and then install its python support:

pip install pynvim --upgrade

In order to check whether it worked, go into nvim and run :checkhealth - it will show you a red warning if Python is not supported and it will tell you what to do.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .