flake8 / vim / python2 / python3

flake8 / vim / python2 / python3

  • Written by
    Walter Doekes
  • Published on

To syntax check Python code before executing, I use flake8. And when coding in the Vim editor, I use the vim-flake8 plugin that allows me to hit <F7> to quickly check for errors in the file I’m currently working in.

But, there are currently two common flavors of Python: python2 and python3. And therefore flake8 comes in two flavors as well — you guessed it — a python2 and a python3 flavor. Unfortunately they are named the same on install: flake8

To fix this, I installed them twice, and then patched vim-flake8 to select the right one, based on the Function key (either F7 or F8).

$ sudo pip install flake8
$ sudo mv /usr/local/bin/flake8{,.2}
$ sudo pip3 install flake8
$ sudo mv /usr/local/bin/flake8{,.3}

I now have two flake8 executables, appropriately named flake8.2 and flake8.3.

Patching the flake8 Vim plugin is then easy as py (hehe):

--- .vim/ftplugin/python/flake8.vim.orig  2015-09-15 10:04:34.680245861 +0200
+++ .vim/ftplugin/python/flake8.vim 2015-09-15 10:10:48.395762116 +0200
@@ -12,11 +12,11 @@ endif
 let b:loaded_flake8_ftplugin=1

 if !exists("*Flake8()")
-    function Flake8()
+    function Flake8(flake8_cmd)
         if exists("g:flake8_cmd")
             let s:flake8_cmd=g:flake8_cmd
-            let s:flake8_cmd="flake8"
+            let s:flake8_cmd=a:flake8_cmd

         if !executable(s:flake8_cmd)
@@ -97,6 +97,7 @@ endif
 " remapped it already (or a mapping exists already for <F7>)
 if !exists("no_plugin_maps") && !exists("no_flake8_maps")
     if !hasmapto('Flake8(')
-        noremap <buffer> <F7> :call Flake8()<CR>
+        noremap <buffer> <F7> :call Flake8("flake8.2")<CR>
+        noremap <buffer> <F8> :call Flake8("flake8.3")<CR>

If you’re in Python2 code, hit <F7>. Are you coding Python3? Hit <F8>.

Back to overview Newer post: scapy / dns server / snippet Older post: python / subprocess / winch