Python pylint use method (pylint code check) _python

Source: Internet
Author: User
Tags install django
Pylint provides a simple way to analyze Python code, and its high level of configuration makes it easy for a department's people to use a Uniform code style. This article will explain how to standardize Python code by Pylint and example analysis.

First, what is Pylint?

Pylint is a Python Code analysis tool that parses errors in Python code and looks for code that does not conform to code style standards and has potential problems.
Pylint is a Python tool that provides additional functionality in addition to the usual Code analysis tools: Checking the length of a line of code, whether a variable name conforms to a naming standard, whether a declared interface is actually implemented, and so on.
One of the great benefits of Pylint is its high configuration, high customization, and the ability to write small plugins to add functionality.

If you run the Pylint two times, it will show both the current and last run results, so you can see if the code quality has improved. The Pylint is now integrated into the eclipse's Pydev plugin.

Second, the installation of Pylint on Linux

1. On Linux, first install the Python package (above version 2.2) and add the path to the Python executable file in the environment variable $PATH.
2. Download the packages for Pylint, logilab-astng (version >= 0.14) and Logilab-common (version >= 0.13), and unzip the packages using tar zxvf *.tar.gz.
3. Go to Logilab-astng, Logilab-common, and Pylint in the Unpacked folder and run the command Python setup.py install.
4. Once the installation is complete, you can call pylint through the Pylint [options] module_or_package.

Iii. installation of Pylint on Windows

1. Install the Python package (above version 2.2), right-click on the My Computer icon on the desktop, select Properties, Advanced, environment variables, add the Python installation path in the $PATH, such as C:\Python26\.
2. Unzip all packages using the Unzip tool.
3. Open the command-line window and use the CD to go to Logilab-astng, Logilab-common, and Pylint to unpack the folder and run the command Python setup.py install.
4. After the installation is complete, a Scripts folder appears under the Python installation path containing some bat scripts, such as Pylint.bat.
5. In order to make the call Pylint.bat do not need to enter the full path, in the Python installation directory to create the Pylint.bat redirect file, which is a plain text file Pylint.bat, which contains pylint.bat the actual path, such as: C # Python26\scripts\pylint.bat.
6. After the installation is complete, you can call Pylint by Pylint [options] module_or_package.

Iv. Use of Pylint

Code check for a module module.py using Pylint:

1. Go to the folder where the module is located, run

pylint [options] module.py

This invocation is always working because the current working directory is automatically added to the Python path.

2. Do not enter the folder where the module is located, run

pylint [options] directory/module.py

This invocation can work when the following conditions are met: Directory is a python package (for example, contains a __init__.py file), or directory is added to the Python search path.

Code check for a package pakage using Pylint:

1. Go to the folder where the package is located, run

pylint [options] pakage

This invocation is always working because the current working directory is automatically added to the Python path.

2. Do not enter the folder where the package is located, run

pylint [options] directory/ pakage

In this case, it works when the following conditions are met: Directory is added to the Python path. For example, on Linux, export pythonpath= $PYTHONPATH: directory.
In addition, for machines with the Tkinter package installed, you can use the command Pylint-gui to open a simple GUI interface, where you enter the name of the module or package (the rules and the command line), and the output of the click Run,pylint is displayed in the GUI.

V. Pylint Common Command Line parameters

-H,--Help: Displays all assistance information.
--generate-rcfile: You can use Pylint--generate-rcfile to generate a sample configuration file. You can use redirection to save this configuration file for later use. You can also precede the other options so that the values of these options are included in the resulting configuration file. such as: Pylint-persistent=n--generate-rcfile > pylint.conf, viewing pylint.conf, you can see Persistent=no, and no longer is its default value yes.
--rcfile=<file>: Specifies a configuration file. Put the configuration in the configuration file, which not only regulates your own code, but also makes it easy to share these specifications with others.
-I <y_or_n>--include-ids=<y_or_n>: Contains the ID of the message in the output and then Pylint--help-msg=<msg-id> to see the details of the error So that you can locate the error in detail.
-R <y_or_n>,--reports=<y_or_n>: The default is Y, which means that the output of Pylint contains the part of the source code analysis, including the report section.
--files-output=<y_or_n>: Outputs each module/package message to a pylint_module/package. [Txt|html] In the named file, if there is a report, output to the name Pylint_global. [txt|html] in the file. The default is output to the screen does not output to the file.
-F <format>,--output-format=<format>: Sets the output format. The formats you can choose are text, parseable, colorized, MSVs (Visual Studio) and HTML, and the default output format is text.
--disable-msg= <msg ids>: Suppresses the message of the specified ID. For example, the output contains the message W0402 this warning, if you do not want it to appear in the output, you can use the--disable-msg= W0402

Vi. output of Pylint

The default output format for Pylint is the raw text format, which can be specified with the-f <format>,--output-format=<format> for other output formats, such as HTML, and so on.

There are two parts in the output of Pylint: The Source Code Analysis section and the reporting section.
Source Code Analysis Section:
For each Python module, the result of Pylint first displays some "*" characters, followed by the name of the module, followed by a series of message, the format of the message is as follows: __message_type:line_num:[object:] MES There are several sage__ Message_type:

(C) practice. Violates coding style standards
(R) refactoring. Code that was badly written.
(W) warning. Some Python-specific issues.
(E) error. Most likely an error in the code.
(F) Fatal error. Errors that prevent Pylint from running further.

Report section:

After the end of the source code analysis, there will be a series of reports, each focusing on some aspects of the project, such as the number of message per category, the dependency of the module, and so on.
Specifically, the report contains the following aspects: The number of module checks. For each module, the percentage of errors and warnings in them.
For example there are two module A and B, if a total of 4 errors are checked out, 1 errors are in a, 3 errors are in B, then A's error percentage is 25%, B's error percentage is 75%. The total number of errors, warnings.

How to use Pylint in Python

Under Eclipse IDE, open window->preferences ... dialog box, select "PyDev", "Interpreter Python" from the tree list on the right, click the New button, select Python.exe from the Python installation path, and a dialog will pop up to let you tick the system PYTHONPATH, I have chosen, it should not matter. Last click OK, exit.

First, the development of the Django project configuration

1. Build Pydev Project

Eclipse ide-->file-->new-->other-->pydev--> Select Pydev project--> the name of a project (for example, demo)-and select the path, such as e:/ Work--> Choose the Python version that's right for you (mine is 2.5)--Remove the following tick and do not create the src file-->finish.

2. Setting up Django Project

(1) django-admin.py Startproject demo. (seemingly unable to use the django-admin.py directly in the command line, but must use the c:/python26/scripts/django-admin.py, later only to know to add c:/python26/scripts path.)

(2) Copy the generated Django project directory into the directory under the project that eclipse has just created. Go back to the Elicpse ide--> in the Pydev package view to refresh the newly created project demo to see the Django Project project. The actions of adding, deleting files and directories can be done by using the right-click menu.

3. Django Project Engineering Configuration

(1) Project right--and properties--Select Pydev-pythonpath-->add source folder (select Project file path to add to project code--OK.)

(2) Configure the Pydev project:

Select the project name for Pydev-->run as-->open Run dialog-->python run--> Right-click to write the project name in the main panel, load the project name via browse and the main Module,main Module Select manage.py--> in the augement panel, enter Runserver--noreload in arguments, and the following working directory to join your base Directory

Second, the configuration of the Django project debugging

Similar to the development configuration, but two environment variables have been added, as follows:
(1) Project right-click-->debug as-->open Debug dialog.
(2) Select the Project,main module where the manage.py is located in the Main window manage.py the file location.
(3) Input runserver--noreload in arguments.
(4) Environment Add Django_settings_module=settings, Pythonpath= $PWD.

Third, configuration Pylint

1, Pylint Introduction

Pylint is mainly used to analyze your py code, find out the errors in it, and give a hint, and you can also suggest some coding style hints for a Python module. In short, it works to make your code closer to the code style described in PEP 008 (http://www.python.org/dev/peps/pep-0008/) Title:style Guide for Python codes, Make your code uniform and more readable.

2. Download and install Pylint

Pylint, logilab-astng, Logilab-common, installation is the same way you would install Django. That is, direct use
Python setup.py install.

3. Configuration using Pylint
(1) Pylint, Pydev, Preferences, window, select "Use Pylint?" and enter the lint.py address, for example "c:/python25/lib/site-packages/ pylint/lint.py "

(2) and add parameters to the final edit box to limit the output of the pylint.

--persistent=n--comment=n

--disable-msg=c0103,c0301,w0312,w0511,w0232,e1101

(3) Project->properties->pydev-pythonpath Add the project's source file directory to "project source Folders".
(4) Check project->build automatically so that Pylint will automatically check the code in the project when saving the changes, otherwise it will use CTRL+B manual Build and trigger Pylint.

Reference Documentation:

Http://pydev.org/manual_adv_pylint.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.