Skip to content

Instantly share code, notes, and snippets.

@uuklanger
Last active December 4, 2023 20:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save uuklanger/fe65dc6da9169c1585abad4ac2b0d268 to your computer and use it in GitHub Desktop.
Save uuklanger/fe65dc6da9169c1585abad4ac2b0d268 to your computer and use it in GitHub Desktop.
HOWTO - Setup pylint with PyCharm and the PyLint Plugin

Overivew

Setting up a linter can help detect odd or non-typical coding practices. The following will describe how to setup PyCharm for basic linting with the PyCharm PyLint plugin.

This assumes you have a venv setup within your current project.

Install Pylint

For pylint for your current project venv the following two commands should be run in your project root. The first will install pylint and the second will create an rc file which you can then adjust for your current project.

% pip3 install pylint
% pylint --generate-rcfile > .pylintrc

I like to edit my .pylintrc file to have longer lines in my files. Just search for max-line-length in the .pylintrc file. I changed my length to 120.

max-line-length=120

Install Pylint Plugin

  1. Open Settings
  2. Search on plugins
  3. In the Marketplace search on pylint
  4. Install and restart the IDE when prompted.

Configuration of Pylint Plugin

To setup the Pylint Plugin for your current project

  1. Open Settings
  2. Search on pylint. You will find a specific setting.
  3. There will be three settings to configure.
Configuration Value
Path to Pylint executable venv/Scripts/pylint OR venv/Scripts/pylint.exe for windows
Path to pylintrc .pylintrc
Arguments --ignore-pattern=venv

Click TEST to ensure the executable can be found. In your .idea directory within your project you will find a new file called pylint.xml that holds these configurations.

Pylint Plugin Usage

You will find a new option under View -> Tools Windows called "Pylint". When clicked, it will open a new Pylint tab in the bottom drawer. See the above link for usage but in general, the "Play" button just lints the current file. There are others for module and project level.

Pylint from the Command Line Usage

Lets say you have a project with the following directories....

  • test/
  • controllers/
  • models/
  • etc/
  • exceptions/
  • repositories/
  • helpers/
  • word_search.py

... To pylint all of them from the command line, you would run.

% pylint --ignore-patterns=venv --msg-template={path}:{line}:{column}:{C}:({symbol}){msg} test controllers models etc exceptions repositories helpers word_search.py

There are many options to refine this as well but this is a common starting point.

If you like the command line approach, you can add it to PyCharm as an External Tool within Settings

Configuration Value
Name Pylint - Current File
Group linters
Description Run Pylint on a selected file within the project.
Program $PyInterpreterDirectory$\pylint
Arguments --ignore-patterns=venv --msg-template="$FileDir${path}:{line}:{column}:{C}:({symbol}){msg}" $FilePath$
Working directory $ContentRoot$

If you click on Tools -> linters -> Pylint - Current File you will see output in a drawer at the bottom of Pycharm. This works nice but the plugin is nicer.

Final Thoughts

This should be enough to get you started with pylint. From here, read the pylint docs and fine tune your configuration.

@vinayak-gagrani
Copy link

I have a folder which has a git repo. at top level there is init.py and .pylintrc. Plugin is not able to correctly pick the PYTHONPATH and keeps complaining cannot import while pycharm is successfully able to import. I have only marked the ProjectDir as Source Roots and Content Roots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment