Adding Pylint to IntelliJ

A switch back to programming in Python, meant I wanted to be able to use Pylint from inside IntelliJ.

I’ve been programming in JavaScript for the last few months, and had IntelliJ IDEA using ESLint to give me context highlighting. It was really easy to setup and I found it useful in helping me stay compliant with our coding style, which differs slightly from my personal style.

I’ve switch back to Python for another project and wanted to use Pylint from within IntelliJ IDEA, for a similar purpose. It turns out that you can’t, at least, you can’t have editor context highlighting. Which is a bit shit.

Yes, IntelliJ IDEA already has pretty good PEP8 context highlighting, but not everyone on the team uses IntelliJ IDEA, let alone an IDE (don’t ask). So I really wanted to get Pylint working in the IDE, so I didn’t have to keep dropping to a terminal every time I wanted to check it.

While IntelliJ IDEA wont do editor context highlighting with Pylint, you can add it as an external tool. This allows it to execute from within the IDE and provide links to any lines that have issues.

I started by following the instructions for PyCharm in the Pylint documentation. The Python plugin for IntelliJ IDEA, provide pretty much the same functionality as PyCharm, so I figured it should work. I couldn’t get it to work though.

Next I found this translation of a Russian blog, but again, I couldn’t quite get it to work. So I started fiddling with all the macros that are available until I found those that worked. So here you go, Pylint from within IntelliJ IDEA and picking up the correct virtualenv.

Start by opening the Preferences, (⌘, on a Mac) and browsing to the Tools > External Tools menu. Select the little symbol from the bottom of the main panel (or ⌘N on a Mac). Fill out the form with similar values to what’s below:

IntelliJ Preferences Edit Tool

Name
Pylint – The name it will appear under the list of external tools.
Description
A Python source code analyzer which looks for programming errors, helps enforcing a coding standard and sniffs for some code smells. – Not sure where this is actually used, but when you edit, you can at least be reminded of why you added it.
Program
$PyInterpreterDirectory$/pylint – Using the $PyInterpreterDirectory$ macro means that it’ll pick up the correct vitrualenv
Parameters
-rn -f parseable $FilePath$-rn means we only want the messages, not the other report gubbings. -f parseable means that it produces something that IntelliJ can parse. $FilePath$ is the full path to the current file.
Working directory
$PyInterpreterDirectory$ – tells Pylint to run from the vitrualenv bin folder, rather than the IntelliJ project folder.

I also unchecked the Main menu and Project views options in the Show in section. Mainly as I only wanted the ability to right click within the currently open file and lint it. To lint whole folders, or the entire project, you’ll need to modify both Parameters and Working directory fields with the correct macros.

This was enough to get it working, but without hyperlinking the any lines with issues. To enable that, click on the Output filter… button and add a new filter:

IntelliJ Preferences Edit Tool Edit Filter

When you right click within a source file, you should now have a pylint option under the External Tools sub menu. Which should give you clickable links in the output.

IntelliJ Pylint Output

.pylintrc

There is only one downside to the configuration outlined above. If you have a .pylintrc file in the root of your project, Pylint can’t find it. This is due to the working directory being set to $PyInterpreterDirectory$, rather than a macro that represents the source root.

Pylint has a specific search order for the .pylintrc file. So you can either use something like the PYLINTRC environment variable. Or, you could just add the location of the file in the Parameters field of the External Tools dialog:

-rn -f parseable --rcfile $ContentRoot$/.pylintrc $FilePath$

That did the trick for me. This means you can have your .pylintrc file under source code control, which is useful if different projects have different requirements.

Making sure IntelliJ uses the correct virtualenv

There is one extra step that you need to be aware of if you want this to work if you are using virtualenv. By default IntelliJ IDEA knows nothing about your virtualenv, so you need to add it as a new SDK for the project. Open the Project Structure dialog (⌘; on a Mac), and add the virtualenv as a new Python SDK. The n you should be up and away.