8

I've scoured related posts and have tried several things, but I'm still receiving a syntax error in Vim when using Python3 syntax.

The code that is receiving the error:

types_of_people = 10; f"There are {types_of_people} types of people."

The output of running $ vim --version:

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 17 2017 10:39:50)
MacOS X (unix) version
Included patches: 1-1100
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     -tcl
-balloon_eval    +folding         +mouse_xterm     +termguicolors
-browse          -footer          +multi_byte      +terminal
++builtin_terms  +fork()          +multi_lang      +terminfo
+byte_offset     -gettext         -mzscheme        +termresponse
+channel         -hangul_input    +netbeans_intg   +textobjects
+cindent         +iconv           +num64           +timers
-clientserver    +insert_expand   +packages        +title
+clipboard       +job             +path_extra      -toolbar
+cmdline_compl   +jumplist        +perl            +user_commands
+cmdline_hist    +keymap          +persistent_undo +vertsplit
+cmdline_info    +lambda          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       -python          +viminfo
+cscope          +lispindent      +python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             +rightleft       +windows
+diff            +menu            +ruby            +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      +startuptime     -xpm
+eval            +mouse_dec       +statusline      -xsmp
+ex_extra        -mouse_gpm       -sun_workshop    -xterm_clipboard
+extra_search    -mouse_jsbterm   +syntax          -xterm_save
+farsi           +mouse_netterm   +tag_binary      
system vimrc file: "$VIM/vimrc"
 user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
  user exrc file: "$HOME/.exrc"
   defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X_UNIX  -g -O2 -
U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: clang   -L. -fstack-protector -L/usr/local/lib -
L/usr/local/opt/libyaml/lib -L/usr/local/opt/openssl/lib -
L/usr/local/opt/readline/lib  -L/usr/local/lib -o vim        -lncurses -liconv 
-framework Cocoa   -mmacosx-version-min=10.11 -fstack-protector-strong -
L/usr/local/lib  -L/usr/local/Cellar/perl/5.26.0/lib/perl5/5.26.0/darwin-
thread-multi-2level/CORE -lperl -lm -lutil -lc  -
L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-
darwin -lpython3.6m -framework CoreFoundation  -lruby.2.4.2 -lobjc 

You may notice above that Vim is including python3 but not python

I have reinstalled Vim using:

$ brew uninstall vim
$ brew install vim --with-python3

I have also tried aliasing python to my Python3 version:

$ alias python='python3.6'

My PATH:

$ echo $PATH  
/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/garythegoat/.rbenv/shims:/usr/bin/python:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin:/Users/garythegoat/golang/bin

:echo has('python') returns 1

:echo has('python3') returns 0

Any suggestions appreciated

5
  • How about a symlink pointing python to python3? Commented Sep 17, 2017 at 18:11
  • Vim doesn't use the python binary; it links against libpython; you can see this in the compile flags: -lpython3.6m. I don't know why your has() calls return something different than your :version. Are you sure you're using the same Vim and not mixing up various vim/macvims or something like that? Commented Sep 18, 2017 at 6:43
  • @Carpetsmoker I am using Macvim, but the syntax error issue is the same in Vim as well. Curiously, the echo statements above in Vim are reversed from Macvim, with has('python') returning 0 and has('python3') returning 1 Commented Sep 18, 2017 at 12:37
  • I am curious what this code is expected to do. types_of_people = 10; f"There are {types_of_people} types of people." should set an integer value in types_of_people and then create and immediately discard a string. Is this a syntax error or is it telling you there is an unassigned string being created? Commented Sep 18, 2017 at 20:29
  • @DanLowe It's a syntax error - it's a contrived example because I wanted to fit it onto one line for the purposes of asking the question Commented Sep 19, 2017 at 13:41

1 Answer 1

1

This is a difficult question, because this is related to the default python configured by your OS.

The most useful related question I found is this one.

Both solutions worked for me :

  1. Setting up a Virtual Environment using virtualenv. This is exhausting as you will have to launch the virtual env each time you need to open vim.

  2. Adding let g:syntastic_python_python_exec = 'python3' to .vimrc configuration file. I'm not sure if your configuration is based on the syntastic plugin, but this is something worst looking into.

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

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.