Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Syntastic as python3 checker instead of python2

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
like image 893
Rainning Avatar asked Dec 07 '16 13:12

Rainning


People also ask

What is the best way to select 2 and 3 Python hashbangs?

Pythons 2 and 3 have some incompatibilities and for a long time the convention was for hashbangs at the top of Python scripts to select 2 with #!/usr/bin/env python and 3 with #!/usr/bin/env python3.

Is it possible to set Python2 as Python?

Question did not say that python scripts are created by user: they may be third party scripts, and in that case we do not want to change all of theirs shebangs, especially if they are under some version control and our changes will get erased when we update the scripts. In such case we really do need to somehow temporarily set python2 as python.

How can I replace default Python with Python3?

instead of: What you could alternatively do is to replace the symbolic link "python" in /usr/bin which currently links to python3 with a link to the required python2/2.x executable. Then you could just call it as you would with python 3. There's really no use for a "default" Python.

Is it possible to set Python2 as Python on Arch Linux?

In such case we really do need to somehow temporarily set python2 as python. Good example of this is nacl sdk from google, which causes problem on archlinux because it expects python to be python2. – Martinsos Apr 1 '15 at 14:50


2 Answers

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.

like image 105
ivan_pozdeev Avatar answered Oct 07 '22 23:10

ivan_pozdeev


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.

like image 20
Sato Katsura Avatar answered Oct 08 '22 01:10

Sato Katsura