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.
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
- Open Settings
- Search on plugins
- In the Marketplace search on
pylint
- Install and restart the IDE when prompted.
To setup the Pylint Plugin
for your current project
- Open Settings
- Search on pylint. You will find a specific setting.
- 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.
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.
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 |
|
Arguments | --ignore-patterns=venv --msg-template="$FileDir${path}:{line}:{column}:{C}:({symbol}){msg}" |
Working directory |
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.
This should be enough to get you started with pylint
. From here, read the pylint docs and fine tune your configuration.
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.