1

I'm working with python 2.7 scripts that I need to update to python 3.8 while maintaining backwards compatibility. vim-flake8 on my mac has been working great up until now for python 2.7, but I can't find a why to easily swap between python 2 and 3 on the fly. This is because Flake8 is "smart" about whether to use 2 or 3 based on which one runs it - there doesn't appear to be an option/argument.

How can I tell vim-flake8 which python version to evaluate my scripts as?

Ideally I would like to have two hotkeys (F2, F3) that allow me to run either, similar to 'python -m flake8' or 'python3 -m flake8'. I could write this directly into my vimrc, but the flake8 plugin has some really nice features to help quickly make fixes. I could not find any mention of swapping python versions on the vim-flake8 github.

  • 2
    Does it work if you use let g:flake8_cmd = "python -m flake8" or let g:flake8_cmd = "python3 -m flake8" before invoking call flake8#Flake8()<CR> in your F2 and F3 mappings? – filbranden Apr 20 at 3:41
  • 1
    That does seem to work to some extent. The error list that pops up is version correct, but the highlighting isn't 100% right. Might not work for everyone, but it'll work well in this context. – Lagix Apr 20 at 17:54
0

This set of commands allowed me to run flake8 through either python 2 or 3 at a whim. It uses the g:flake8_cmd command as recommended in the comments by filbranden and it is in fact listed on the vim-flake8 github.

This isn't 100% however because there is some odd behavior. For one the highlighting seems slightly off; but vim-flake also didn't error handle quite right as initially vim couldn't find python3 but was still saying Flake8 check OK.

function Python2Flake()
   let g:flake8_cmd = "python -m flake8"
   call flake8#Flake8()

:endfunction

autocmd Filetype python map <buffer> <F2> :call Python2Flake()

function Python3Flake()
    let g:flake8_cmd = "python3 -m flake8"
    call flake8#Flake8()

:endfunction

autocmd Filetype python map <buffer> <F3> :call Python3Flake()
| improve this answer | |

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.