如何将 Syntastic 设置为 python3 检查器而不是 python2

How to setup Syntastic as python3 checker instead of python2

在 MacVim 中,我将以下代码保存为 test.py

print "Hello world! python2"

,这显然与 python3 是错误的,但是 在我 运行 :w 保存文件后,没有错误消息, 以下是 ~/.vimrc 的一部分,都是关于 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']

如何让 Syntastic 像我在终端中 运行 test.py 一样检测此类错误:

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

以下是 :SyntasticInfo 所说的内容:

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

flake8 是一个 Python 包。它使用 Python 的内置工具来解析代码,so it accepts syntax for the Python version that it belongs to

如何为您的 python3 安装取决于安装本身的安装方式 - unless you're fine with using pip

来自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.