How to setup Syntastic as python3 checker instead of python2
Asked Answered
L

2

11

In MacVim, I save following code as test.py

print "Hello world! python2"

, which is apparently wrong with python3, but after I run :w to save the file, there is no error message for it, Following is a part of ~/.vimrc, which is all about Syntastic:

" Syntastic                                                                     
"" Recommended settings                                                         
set statusline+=%#warningmsg#                                                   
set statusline+=%{SyntasticStatuslineFlag()}                                    
set statusline+=%*                                                              
let g:syntastic_always_populate_loc_list = 1                                    
let g:syntastic_auto_loc_list = 1                                               
let g:syntastic_check_on_open = 1                                               

"" Display checker-name for that error-message                                  
let g:syntastic_aggregate_errors = 1        

"" I use the brew to install flake8                                             
let g:syntastic_python_checkers=['flake8', 'python3']

How to make Syntastic detect this type of error as I run test.py in Terminal:

NingGW:Desktop ninggw$ python3 test.py
  File "test.py", line 1
    print "Hello world! python2"
                               ^
SyntaxError: Missing parentheses in call to 'print'

Following is what :SyntasticInfo said:

Syntastic version: 3.8.0-10 (Vim 800, Darwin, GUI)
Info for filetype: python
Global mode: active
Filetype python is active
The current file will be checked automatically
Available checkers: flake8 python
Currently enabled checker: flake8
Press ENTER or type command to continue
Leprosarium answered 7/12, 2016 at 13:37 Comment(1)
Which Python installation does your flake8 belong to? I think this is the key.Heyduck
H
6

flake8 is a Python package. It uses Python's built-in facilities to parse code, so it accepts syntax for the Python version that it belongs to.

How to install it for your python3 installation depends on how that installation itself was installed - unless you're fine with using pip.

Heyduck answered 7/12, 2016 at 15:12 Comment(2)
Oh, I just got it, brew install the flake8 to my python2! I reinstalled it with pip3 now...Leprosarium
It works! thanks! before I got your answer I tried pylint to solve it temporarily, now both pylint and flake8 point out the error, the former said test.py|1 col 1 error| [syntax-error] Missing parentheses in call to 'print' [python/pylint] and the latter test.py|1 col 21 error| SyntaxError: invalid syntax [E999] [python/flake8]Leprosarium
C
2

From the FAQ:

4.11. Q. How can I check scripts written for different versions of Python?

A. Install a Python version manager such as virtualenv or pyenv, activate the environment for the relevant version of Python, and install in it the checkers you want to use. Set g:syntastic_python_checkers accordingly in your vimrc, and run Vim from the virtual environment.

If you're starting Vim from a desktop manager rather than from a terminal you might need to write wrapper scripts around your checkers, to activate the virtual environment before running the actual checks. Then you'll need to point the relevant g:syntastic_python_<checker>_exec variables to the wrapper scripts.

Carbarn answered 7/12, 2016 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.