0

I am running Ubuntu 16.04, which requires the Python 3 system version to be 3.5: updating it to 3.7 breaks the package manager.

At the same time, I want to use Vim with YouCompleteMe. YouCompleteMe has stopped supporting Python 3.5, and now requires Python 3.7.

If I change system's default python3 to 3.7 (using update-alternatives) YouCompleteMe works but the package manager doesn't; if I keep it at 3.5 it's the other way around.

How can I have Vim use Python 3.7 while keeping the rest of the system defaulting to 3.5?

I have tried compiling Vim from source, and using the following configuration, but no luck:

./configure \
        --with-features=huge \
        --enable-multibyte \
        --enable-rubyinterp=yes \
        --enable-python3interp=yes \
        --with-python3-command=python3.7 \
        --with-python3-config-dir=$(python3.7-config --configdir) \
        --enable-perlinterp=yes \
        --enable-luainterp=yes \
        --enable-gui=gtk2 \
        --enable-cscope \
        --prefix=/usr/local

When running vim --version I still see that it is linked against python3.5m:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jul 30 2020 11:32:35)
Included patches: 1-1325
Compiled by XXX Huge version with GTK2 GUI.  Features included (+) or not (-):
+acl               -farsi             +mouse_sgr         +tag_binary
+arabic            +file_in_path      -mouse_sysmouse    -tag_old_static
+autocmd           +find_in_path      +mouse_urxvt       -tag_any_white
+autochdir         +float             +mouse_xterm       -tcl
-autoservername    +folding           +multi_byte        +termguicolors
+balloon_eval      -footer            +multi_lang        +terminal
+balloon_eval_term +fork()            -mzscheme          +terminfo
+browse            +gettext           +netbeans_intg     +termresponse
++builtin_terms    -hangul_input      +num64             +textobjects
+byte_offset       +iconv             +packages          +textprop
+channel           +insert_expand     +path_extra        +timers
+cindent           +ipv6              -perl              +title
+clientserver      +job               +persistent_undo   +toolbar
+clipboard         +jumplist          +popupwin          +user_commands
+cmdline_compl     +keymap            +postscript        +vartabs
+cmdline_hist      +lambda            +printer           +vertsplit
+cmdline_info      +langmap           +profile           +virtualedit
+comments          +libcall           -python            +visual
+conceal           +linebreak         +python3           +visualextra
+cryptv            +lispindent        +quickfix          +viminfo
+cscope            +listcmds          +reltime           +vreplace
+cursorbind        +localmap          +rightleft         +wildignore
+cursorshape       -lua               +ruby              +wildmenu
+dialog_con_gui    +menu              +scrollbind        +windows
+diff              +mksession         +signs             +writebackup
+digraphs          +modify_fname      +smartindent       +X11
+dnd               +mouse             -sound             -xfontset
-ebcdic            +mouseshape        +spell             +xim
+emacs_tags        +mouse_dec         +startuptime       -xpm
+eval              -mouse_gpm         +statusline        +xsmp_interact
+ex_extra          -mouse_jsbterm     -sun_workshop      +xterm_clipboard
+extra_search      +mouse_netterm     +syntax            -xterm_save    

     system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
     2nd user vimrc file: "~/.vim/vimrc"
     user exrc file: "$HOME/.exrc"
     system gvimrc file: "$VIM/gvimrc"
     user gvimrc file: "$HOME/.gvimrc"
     2nd user gvimrc file: "~/.vim/gvimrc"
     defaults file: "$VIMRUNTIME/defaults.vim"
     system menu file: "$VIMRUNTIME/menu.vim"
     fall-back for $VIM: "/usr/local/share/vim"

Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread
    -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include
    -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0
    -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1
    -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12
    -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0
    -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
    -I/usr/include/freetype2   -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       

Linking: gcc   -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    -fstack-protector -rdynamic -Wl,-export-dynamic  -L/usr/local/lib
    -Wl,--as-needed -o vim   -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0
    -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0
    -lglib-2.0 -lfontconfig -lfreetype -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE
    -lm -ltinfo -lelf  -lselinux  -lacl -lattr -ldl
    -L/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu -lpython3.5m -lcrypt
    -lpthread -ldl -lutil -lm  -lruby-2.3 -lpthread -lgmp -ldl -lcrypt -lm  
  • This probably belongs on another Stack Exchange site. Look into the pyenv tool or equivalent. That lets you switch easily between different versions of Python – B Layer Jul 30 at 9:45
  • @BLayer I considered that, but I thought it might be possible to somehow configure it from vimrc or something similar, so I wanted to give it a shot. – user2891462 Jul 30 at 9:50
  • Ah, okay. Well at least you have a fallback plan. – B Layer Jul 30 at 9:50
1

You can compile Vim from source to configure which interpreter it should use. The full instructions can be found on the YouCompleteMe wiki.

It turns out the method described in the original post is correct: configuring the installation with the following options should have Vim link against Python 3.7

./configure \
        --enable-python3interp=yes \
        --with-python3-command=python3.7 \
        --with-python3-config-dir=$(python3.7-config --configdir) \
        ... other options

The reason this was not happening was that there were leftovers from previous installations/compilations. Cleaning the compilation by running make clean distclean and removing any current Vim installations (using apt or dpkg if they were installed from the repositories or from source using checkinstall) should make the following steps work correctly:

$ ./configure --with-features=huge \
            --enable-multibyte \
            --enable-rubyinterp=yes \
            --enable-python3interp=yes \
            --with-python3-command=python3.7 \
            --with-python3-config-dir=$(python3-config --configdir) \
            --enable-perlinterp=yes \
            --enable-luainterp=yes \
            --enable-gui=gtk2 \
            --enable-cscope \
            --prefix=/usr/local

$ make VIMRUNTIMEDIR=/usr/local/share/vim/vim82
$ sudo checkinstall

This is the new result:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jul 30 2020 11:49:11)
Included patches: 1-1325
Compiled by XXXX
Huge version with GTK2 GUI.  Features included (+) or not (-):
+acl               -farsi             +mouse_sgr         +tag_binary
+arabic            +file_in_path      -mouse_sysmouse    -tag_old_static
+autocmd           +find_in_path      +mouse_urxvt       -tag_any_white
+autochdir         +float             +mouse_xterm       -tcl
-autoservername    +folding           +multi_byte        +termguicolors
+balloon_eval      -footer            +multi_lang        +terminal
+balloon_eval_term +fork()            -mzscheme          +terminfo
+browse            +gettext           +netbeans_intg     +termresponse
++builtin_terms    -hangul_input      +num64             +textobjects
+byte_offset       +iconv             +packages          +textprop
+channel           +insert_expand     +path_extra        +timers
+cindent           +ipv6              -perl              +title
+clientserver      +job               +persistent_undo   +toolbar
+clipboard         +jumplist          +popupwin          +user_commands
+cmdline_compl     +keymap            +postscript        +vartabs
+cmdline_hist      +lambda            +printer           +vertsplit
+cmdline_info      +langmap           +profile           +virtualedit
+comments          +libcall           -python            +visual
+conceal           +linebreak         +python3           +visualextra
+cryptv            +lispindent        +quickfix          +viminfo
+cscope            +listcmds          +reltime           +vreplace
+cursorbind        +localmap          +rightleft         +wildignore
+cursorshape       -lua               +ruby              +wildmenu
+dialog_con_gui    +menu              +scrollbind        +windows
+diff              +mksession         +signs             +writebackup
+digraphs          +modify_fname      +smartindent       +X11
+dnd               +mouse             -sound             -xfontset
-ebcdic            +mouseshape        +spell             +xim
+emacs_tags        +mouse_dec         +startuptime       -xpm
+eval              -mouse_gpm         +statusline        +xsmp_interact
+ex_extra          -mouse_jsbterm     -sun_workshop      +xterm_clipboard
+extra_search      +mouse_netterm     +syntax            -xterm_save

   system vimrc file: "$VIM/vimrc"
   user vimrc file: "$HOME/.vimrc"
   2nd user vimrc file: "~/.vim/vimrc"
   user exrc file: "$HOME/.exrc"
   system gvimrc file: "$VIM/gvimrc"
   user gvimrc file: "$HOME/.gvimrc"
   2nd user gvimrc file: "~/.vim/gvimrc"
   defaults file: "$VIMRUNTIME/defaults.vim"
   system menu file: "$VIMRUNTIME/menu.vim"
   fall-back for $VIM: "/usr/local/share/vim"

Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread
    -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include
    -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0
    -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1
    -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12
    -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0
    -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
    -I/usr/include/freetype2   -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       

Linking: gcc   -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    -fstack-protector -rdynamic -Wl,-export-dynamic  -L/usr/local/lib
    -Wl,--as-needed -o vim   -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0
    -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0
    -lglib-2.0 -lfontconfig -lfreetype -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE
    -lm -ltinfo -lelf  -lselinux  -lacl -lattr -ldl
    -L/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu -lpython3.7m -lcrypt
    -lpthread -ldl -lutil -lm  -lruby-2.3 -lpthread -lgmp -ldl -lcrypt -lm 
| improve this answer | |
0

You can also have multiple versions of python installed on your system and easily switch between them using pyenv

https://github.com/pyenv/pyenv

| improve this answer | |
  • I am aware of pyenv, and use it to have different Python versions in different projects. However, I don't see how to easily use it for this purpose: Vim (when installed from the repositories) will use the system-wide python3 interpreter. My issue is not with having different Python versions installed, I have 3.5 and 3.7 coexist no problem. How could pyenv be used to ensure Vim (and only Vim) uses the 3.7 interpreter when python3 in the system is 3.5? – user2891462 Aug 1 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.