Sublime Forum

How to set arguments to flake8 as python-linter in Sublime?

#1

New to Sublime and trying to get flake8 python linter to work with Sublime …

TL/DR: no luck with changing to useful settings in Sublime.


(0)
I’ve installed flake8 using:

pip install flake8
both in my virtualenv (at /Users/paul.keyser/task-status-updater/.venv/bin/flake8) and also globally (at /Library/Frameworks/Python.framework/Versions/2.7/bin/flake8). In the virtualenv, I can run it on some python (whole directory or just one file), and it will print errors, like “./tests.py:107:100: E501 line too long (124 > 99 characters)”.

After playing around for a long while, I found Tools => Command Palette => (dialog opens) => (search for “install”) => “Package Control: Install Package” => (new dialog) => (search for “flake8”) => click to install. The result of that is:

…$ ls ~/Library/Application\ Support/Sublime\ Text\ 3/Installed\ Packages/*
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/0_package_control_loader.sublime-package
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/Package Control.sublime-package
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/SublimeLinter-flake8.sublime-package

(See also below, longer listing from related directory)

So I think it is installed correctly …

(1)
I played around with the syntax-specific settings in:
ls ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/Py*
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/User/Python.sublime-settings, and they now look like this (no idea if these are right, but using the global-install path “/Library/Frameworks/Python.framework/Versions/2.7/bin/flake8” makes no difference):
(apologies, cannot get the editor display this correctly…)

{
“paths”: {
“linux”: [],
“osx”: [
“/Users/paul.keyser/task-status-updater/.venv/bin/flake8”
],
“windows”: []
},
“settings”:
{
“SublimeLinter.linters.flake8.disable”: false
}
}

I found out that to turn on the linter, you must save the file. But then I see a bunch of stuff I don’t want, and not stuff I do want: triggers on the default line-length; includes gazillions of D errors.

To adjust the max-line-length, I tried to Edit the Project and add to the JSON file:

"SublimeLinter":
{
    "linters":
    {
        "flake8": {
            "disable": false,
            "args": [
                "--exclude=__pycache__,.pyc",
                "--max-line-length=99",
                "--select=E,F,W,C90",
            ],
        }
    }
}

Neither the “max-line-length” nor the “select” has any effect that I can detect.

How is this actually done? (Yes, I’ve tried quit and restart, many times.)

If the flake8 integration is broken, can you tell me what Python-linter’s integration is not broken, and I’ll just use that?

thanks,
Paul


…$ ls ~/Library/Application\ Support/Sublime\ Text\ 3/Installed\ Packages/*
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/0_package_control_loader.sublime-package
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/Package Control.sublime-package
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/SublimeLinter-flake8.sublime-package
(.venv) C02WQ2LVHTD6:src paul.keyser$
(.venv) C02WQ2LVHTD6:src paul.keyser$
(.venv) C02WQ2LVHTD6:src paul.keyser$
(.venv) C02WQ2LVHTD6:src paul.keyser$ ls ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Python\ Flake8\ Lint/*
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Default (Linux).sublime-keymap
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Default (OSX).sublime-keymap
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Default (Windows).sublime-keymap
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Default.sublime-commands
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Flake8Lint.py
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Flake8Lint.sublime-settings
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/Main.sublime-menu
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/README.md
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/color_theme.py
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/lint.py
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/messages.json
/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/package-metadata.json

/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/contrib:
elementtree_contrib flake8_debugger.py mccabe.py pep8ext_naming.py pyflakes
flake8 flake8_import_order pep8.py pydocstyle.py

/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/gutter-themes:
LICENSE alpha-warning.png bright-warning.png dark-warning.png hard-warning.png simple-warning.png
alpha-critical.png bright-critical.png dark-critical.png hard-critical.png simple-critical.png success.png
alpha-error.png bright-error.png dark-error.png hard-error.png simple-error.png

/Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Python Flake8 Lint/messages:
1.4.0.txt 1.5.0.txt 2.0.0.txt 2.4.0.txt install.txt

0 Likes

#2

Your SublimeLinter settings look correct. I believe the issue is that you have two plugins that use flake8 installed.

Python Flake8 Lint is not related to SublimeLinter, and it looks like it it’s deprecated. I would try using Package Control to uninstall that. That should just leave you with SublimeLinter and the SublimeLinter-flake8 utility package, and now your settings should behave as expected.

0 Likes

#3

OK, removed the “Python Flake8 Lint” using Tools ==> Command Palette ==> (search for “remove”) ==> Remove Pacakge ==> (select “Python Flake8 Lint”) ==> restart Sublime …

And now nothing happens at all. No display of anything about long lines, or anything about extra spaces around " = " when using explicit parameter-names in call-sites, etc.

So how do I get the SublimeLinter to actually do anything?

thanks,
Paul

0 Likes

#4

PS, tried:

…$ pip uninstall flake8
<snip>
…$ pip3 install flake8
…$ which flake8
/usr/local/bin/flake8

Then I set the syntax-specific (python) preferences to look like :

{
	"paths": {
	    "linux": [],
	    "osx": [
	        "/usr/local/bin/flake8"
	    ],
	    "windows": []
	},
	"settings":
    {
    	"SublimeLinter.linters.flake8.disable": false,
        "SublimeLinter.linters.flake8.args": [
            "--exclude=__pycache__,.pyc,.direnv",
            "--max-line-length=99",
            "--select=E,F,W,C90",
        ],
    }
}

Restart Sublime, still no evidence of any linter at all.

Oh, and I tested the installs of flake8, they are fine:

(.venv) task-status-updater/src paul.keyser$ which flake8
/Users/paul.keyser/task-status-updater/.venv/bin/flake8
(.venv) task-status-updater/src paul.keyser$ flake8 --max-line-length=99
./tests.py:76:100: E501 line too long (118 > 99 characters)
./tests.py:96:30: E251 unexpected spaces around keyword / parameter equals
./tests.py:96:32: E251 unexpected spaces around keyword / parameter equals
(.venv) task-status-updater/src paul.keyser$ deactivate 

task-status-updater/src paul.keyser$ which flake8
/usr/local/bin/flake8
task-status-updater/src paul.keyser$ flake8 --max-line-length=99
./tests.py:76:100: E501 line too long (118 > 99 characters)
./tests.py:96:30: E251 unexpected spaces around keyword / parameter equals
./tests.py:96:32: E251 unexpected spaces around keyword / parameter equals

-Paul

0 Likes

#5

Here’s what I put in my User SublimeLinter.sublime-settings file (not mucking with the syntax-specific ones right now, I’m not entirely sure SublimeLinter will look for the settings there):

{
    "debug": true,
    "linters":
    {
        "flake8": {
            "disable": false,
            "args": [
                "--exclude=__pycache__,.pyc",
                "--max-line-length=99",
                "--select=E,F,W,C90",
            ],
        }
    }
}

This works as expected: it runs flake8 with the specified arguments. The “debug” option will print the output from and command executed by SublimeLinter to the Sublime console, which you can access with Ctrl+` (or it might be Command on Mac, I forget). You can check that to see if it’s finding the executable in the first place. I think if you add the “paths” settings into this file as well, it should find the executable.

This has been a good opportunity to read the docs for the changes to the SublimeLinter syntax since I last had to change my configuration…

0 Likes

#6

Well, I had no file at the path /Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/User/SubimeLinter.sublime-settings, so I created one and copy-pasted your code (above) plus my prior attempts at paths (first with /usr/local/bin/flake8); restart Sublime. Still no evidence of any linter.

I should tried the console before :slight_smile: – in Mac it is CTRL-backtick (though I would have guessed CMD-backtick too):

reloading plugin SublimeLinter-flake8.linter
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 116, in reload_plugin
    m = importlib.import_module(modulename)
  File "./python3.3/importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 1183, in load_module
    exec(compile(source, source_path, 'exec'), mod.__dict__)
  File "/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/SublimeLinter-flake8.sublime-package/linter.py", line 1, in <module>
ImportError: No module named 'SublimeLinter'
plugins loaded
Package Control: Skipping automatic upgrade, last run at 2018-07-19 14:18:07, next run at 2018-07-19 15:18:07 or after
using gpu buffer for window
Unable to open /Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings

All right, so does that mean the path /usr/local/bin/flake8 fails? Or that there is a (large) missing step in my setup? Why is it trying to open something in Default, which does not exist on my disk?

So, I try also the path /Users/paul.keyser/task-status-updater/.venv/bin/flake8 (restart Sublime) ==> different traceback but similar conclusion:

reloading plugin SublimeLinter-flake8.linter
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 116, in reload_plugin
    m = importlib.import_module(modulename)
  File "./python3.3/importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 1183, in load_module
    exec(compile(source, source_path, 'exec'), mod.__dict__)
  File "/Users/paul.keyser/Library/Application Support/Sublime Text 3/Installed Packages/SublimeLinter-flake8.sublime-package/linter.py", line 1, in <module>
ImportError: No module named 'SublimeLinter'

Now what?

-Paul

(also, when I open the preferences, the file /Users/paul.keyser/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings is auto-opened (in the RHS of the split screen); its contents, not created by me, are:

{
	"ignored_packages":
	[
		"Vintage"
	]
}

)

0 Likes

#7

PS – and then later on, after closing the project, quitting Sublime, and restarting, etc., opening the project: the console now shows no activity at all.

0 Likes

#8

You need to install the “SublimeLinter” package as well. SublimeLinter-flake8 is a plug in for SublimeLinter, which itself is a linting framework.

1 Like

#9

Ah, I did not understand that.

Now it works:

thanks,
Paul

0 Likes

#10

For the record. here is how I set up flake8 for Sublime Text on my Mac:

  1. install Sublime Linter and Sublime Linter-flake8
  2. Select Preferences>Package Settings>Sublime Linter>Settings

you should now see two windows side by side. The right window is the user prefs. Mine looks like the following (after I removed settings for some other linters) where I have elided most of the path to flake8:

// SublimeLinter Settings - User
{
    "linters": {
        "flake8": {
            "executable": ".../bin/flake8",
            "disable": false,
        },
    },
    "show_panel_on_save": "view"
}
  1. Set my flake8 preferences in the standard location: ~/.config/flake8
    Mine look like this:
[flake8]
# from https://developer.lsst.io/python/testing.html#enabling-additional-pytest-options-flake8
ignore = E133, E226, E228, N802, N803, N806
max-line-length = 110
# might want this if you get too much whining:
# max-complexity = -1
# hide files that use from foo import *:
excludes __init__.py

This centralizes the configuration for flake8, which is useful if you run it from the command line (a very handy way to lint a batch of files) or from other editors.

0 Likes