Thursday 2 February 2017

Integrate pylint into git hook and pycharm

What is pylint:


Pylint is a source code, bug and quality checker for the Python programming language. It follows the style recommended by PEP 8, the Python style guide.[4] It is similar to Pychecker and Pyflakes, but includes the following features:
  • Checking the length of each line
  • Checking if variable names are well-formed according to the project's coding standard
  • Checking if declared interfaces are truly implemented.

Installing pylint:

Install following pylint package using pip installer

mac/unix:

    pip install pylint

windows:

   python -m pip install pylint

Once you installed pylint into your system, check the pylint version using following command to make sure pylint was installed properly or not.

 pylint --version

pylint example.py

Configure pylint into git hook:


Pre-commit hook for Git checking Python code quality. The hook will check files ending with .py or that has a she bang (#!) containing python.
The script will try to find pylint configuration files in the order determined by pylint. It also looks for a [pre-commit-hook] section in the pylint configuration for commit hook specific options.
pip install git-pylint-commit-hook
Next go to your git initialized folder and navigate into .git/hooks/ directory. Rename the existing template file "pre-commit.sample" into "pre-commit"
Delete everything in that file and paste this in the pre-commit file
#!/bin/sh
git-pylint-commit-hook

Usage

The commit hook will automatically be called when you are running git commit. If you want to skip the tests for a certain commit, use the -n flag,
 git commit -n.

pylint configuration

Settings are loaded by default from the .pylintrc file in the root of your repo.
[pre-commit-hook]
command=custom_pylint
params=--rcfile=/path/to/another/pylint.rc
limit=8.0
command is for the actual command, for instance if pylint is not installed globally, but is in a virtualenv inside the project itself.
params lets you pass custom parameters to pylint
limit is the lowest value which you want to allow for a pylint score. Any lower than this, and the script will fail and won’t commit.

Integrate Pylint into pycharm IDE:


step 1: 
Go to file -> settings 


Step2:

select "Tools -> External tools-> click add icon"



Step 3:

Fill the tool setting parameters. To be a little more flexible, you can use PyCharm macros. As an example use the value “$FilePath$” for Working directory and “$Promt$” for Parameters. This allows the use in other projects, too.



Step 4:

Now pylint is configured into your system. Right click the file and select pylint from external tools to run pylint for specific files