Descriptive flake8 errors in PyCharm
Asked Answered
V

5

31

PyCharm does not have a built-in support for flake8 at the moment. But, flake8 can be configured to run as an external tool.

Sometimes, especially for Python newcomers, not every flake8 warning is understandable and additional clarification is required.

We've recently stumbled upon the Flake8Rules project which attempts to describe every single warning in a detailed way with supportive examples.

Is there a way to combine PyCharm, flake8 and Flake8Rules altogether to have static code analysis warnings displayed with additional descriptions or links to the Flake8Rules catalog?

Vive answered 2/1, 2018 at 18:58 Comment(0)
V
43

It is definitely possible.

One approach would be to adjust the flake8 output using the --format command-line option to specify http(s) links to the Flake8Rules catalog:

--format='%(path)s:%(row)d,%(col)d:%(code)s:%(text)s:https://lintlyci.github.io/Flake8Rules/rules/%(code)s.html'

The problem then is for the console or PyCharm output window to render the links properly.

Fortunately, we can do that using the plugins - "Awesome Console" for the terminal and "Console Link" for the output window.


Step-by-step Instructions

  1. make sure to have flake8 installed in the current Python environment
  2. install "Awesome Console" plugin:
  • go to PyCharm Preferences -> Plugins -> Browser Repositories...
  • find "Awesome Console" and install (PyCharm restart required): enter image description here
  1. configure "flake8" as an External Tool:
  • go to PyCharm Preferences -> Tools -> External Tools -> "+"
  • configure the path to flake8 as well as $FilePath$ placeholder for the desired directory/path to be processed: enter image description here

Demo

Now, let's say we have created this test.py file with a few violations:

def f(a = 10):
  return a*10

If we right-click on a test.py file, select External Tools -> flake8, this is the output we are going to get (note the clickable links for every warning):

enter image description here

Now, whenever in doubt, we can follow the link for additional details about a warning.

This is just one way to do it, would be happy to hear if there is an easier or better way to combine these tools and projects.

Vive answered 2/1, 2018 at 18:58 Comment(3)
Awesome console is just AWESOME <3Berna
I think it's $FilePath$ instead of $FileDir$/$FileName$.Fimble
I tried to use this for Windows and a conda environment today got this error: Error running 'flake8' Cannot run program "C:\Users\Steve\anaconda3\envs\lmageProcessing\Lib\site-packages\flake8_main_.py" (in directory "C:\Users\Steve\PycharmProjects\lmageProcessing\src"): CreateProcess error=193, %1 is not a valid Win32 applicationNutritive
M
27

Today i Also face this problem although @alecxe answer is good for one project settings

If you want to set flake8 globally , you can follow below process

  1. make sure flake8 installed in your project
  2. make sure virtualenv path set in pycharm
  3. configure flake8 as external tool goto file> settings> (Tools) > External Tools > '+'

configure the path enter image description here

Program - The path to the flake8 executable $PyInterpreterDirectory$ is a directory where the Python interpreter of the current project is placed

Argument- Specifies what files and folders should be checked $FilePath$

Working directory - Project root directory $ContentRoot$

Mendelssohn answered 25/11, 2019 at 15:12 Comment(1)
To add a Keyboard Shortcut go to: Settings -> Keymap -> External Tools -> Flake8 Do a right click on Flake8 and select Add Keyboard Shortcut and select e.g. Alt+FUdall
B
7

To all the folks, who need:

  • run against files using a remote python interpreter with SSH
  • who want only flake8 on changed files regarding git status
  • who want to pass any other flake8 arguments without pain

To setup that kind of tool in Pycharm:

File | Settings | Tools | Remote SSH External Tools

see below screen for example configuration: enter image description here

Arguments: -c "flake8 $(git status -s | grep -E '\.py$' | cut -c 4-) --max-line-lengt=120"

In my case the crucial thing was:

  1. use /bin/bash instead of flake8 directly
  2. Filling the arguments section by -c "whatever args subcommands etc I need here"

For reference:

NOTE: to have your flake8 from virtualenv you might want to specify full path like: /z/your_virtual_envs/bin/flake8

Berna answered 17/9, 2019 at 12:20 Comment(0)
B
5

I've wrote a library that offer - kind of native flake8 integration with pycharm. The library work by pretending to be a pylint executable. It accept pylint arguments and translate it to flake8 counterpart. Similarly it translate flake8 output to equivalent pylint output.

Result is native flake8 support.

https://gitlab.com/ramast/flake8-for-pycharm/

Banal answered 19/5, 2020 at 13:56 Comment(1)
Pretty much impossible. I am not a java developer so I can't make changes to existing plugins or create a new one. This library i wrote was written in python and it depend on pylint plugin for it to work. There is no way such script can be accepted into market placeBanal
C
4

Here's the approach I used, we basically need to tell flake8 how to output it's errors and tell pycharm how to interpret them. And then run the job when a file changes.

  • Install the 'File Watcher' plugin if not already

  • Click '+' under 'Tools' > 'File Watcher'

  • Create something that looks like the following. We're basically telling flake8 how to format it's output and how pycharm should interpret it here (see this and this) screenshot

  • flake8 (this assumes it's installed globally)

  • --format="ERROR: %(path)s[%(row)d, %(col)d]: %(text)s (%(code)s)" $FilePath$

  • $FilePath$

  • ERROR: $FILE_PATH$\[$LINE$, $COLUMN$\]: $MESSAGE$

  1. Now under 'Editor' > 'Inspections' make sure 'File Watcher Problems' is enabled
Convolution answered 24/8, 2022 at 3:3 Comment(2)
This worked perfectly for me on Windows 11 and PyCharm 2022.2.1 (Community Edition). Only a minor change: I used $ContentRoot$ in the Working Directory field, instead of $FilePath$Hiers
Very good guide about this approach (in Russian, but Google Translate is your friend): tirinox.ru/flake8-pycharmAthamas

© 2022 - 2024 — McMap. All rights reserved.