Home Backend Development Python Tutorial How to use pylint in python (pylint code inspection)_python

How to use pylint in python (pylint code inspection)_python

Apr 08, 2018 am 11:15 AM
pylint python Instructions

Pylint provides a simple way to analyze Python code, and its high configurability makes it easy for people in a department to use a unified coding style. This article will explain how Pylint can standardize Python code through theoretical introduction and example analysis

1. What is Pylint

Pylint is a Python code analysis tool that analyzes Python code Errors in the code, looking for code that doesn't meet coding style standards and has potential problems.
Pylint is a Python tool. In addition to the functions of ordinary code analysis tools, it provides more functions: such as checking the length of a line of code, whether the variable name conforms to the naming standard, and whether a declared interface is actually implemented. etc.
A big benefit of Pylint is that it is highly configurable, highly customizable, and it is easy to write small plug-ins to add functionality.

If you run Pylint twice, it will display the results of the current and last runs at the same time, so you can see whether the code quality has been improved. Pylint is currently also integrated in the pydev plug-in of eclipse.

2. Installation of Pylint on Linux

1. On Linux, first install the Python package (higher than version 2.2) and set it in the environment variable $PATH Add the path to the Python executable file.
2. Download the packages of Pylint, logilab-astng (version >= 0.14) and logilab-common (version >= 0.13), and use tar zxvf *.tar.gz to decompress these packages.
3. Enter the folders where logilab-astng, logilab-common and Pylint were extracted in order, and run the command Python setup.py install to install.
4. After the installation is complete, you can call Pylint through pylint [options] module_or_package.

3. Installation of Pylint on Windows

1. Install the Python package (higher than version 2.2), right-click the My Computer icon on the desktop, Select Properties, Advanced, Environment Variables, and add the Python installation path to $PATH, such as C:\Python26\.
2. Use the decompression tool to decompress all packages.
3. Open the command line window, use cd to enter the unpacked folders of logilab-astng, logilab-common and Pylint in sequence, and run the command python setup.py install to install.
4. After the installation is completed, a Scripts folder will appear in the Python installation path, which contains some bat scripts, such as pylint.bat, etc.
5. In order to avoid entering the full path when calling pylint.bat, create a redirection file of pylint.bat in the Python installation directory. This is a plain text file pylint.bat, which contains pylint.bat. Actual path, such as: C:\Python26\Scripts\pylint.bat.
6. After the installation is complete, you can call Pylint through pylint [options] module_or_package.

4. Use pylint

Use Pylint to check the code of a module module.py:

1. Enter the folder where the module is located, Run

pylint [options] module.py

This calling method will always work, because the current working directory will be automatically added to the Python path.

2. Without entering the folder where the module is located, run

pylint [options] directory/module.py

This method of calling is as follows It can work when the conditions are met: directory is a Python package (such as containing an __init__.py file), or directory is added to Python's search path.

Use Pylint to check the code of a package pakage:

1. Enter the folder where the package is located and run

pylint [options] pakage

This calling method can always work, because the current working directory will be automatically added to the Python path.

2. Without entering the folder where the package is located, run

pylint [options] directory/ pakage

In this case, when the following conditions are met It works: 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, enter the name of the module or package here (the rules are the same as the command line), click Run, and the output of Pylint will be in the GUI displayed in.

5. Common command line parameters of Pylint

-h,--help: Display all help information.
--generate-rcfile: You can use pylint --generate-rcfile to generate an example configuration file. You can use redirection to save this configuration file for later use. You can also add other options in front so that the values ​​of these options are included in the generated configuration file. For example: pylint -- persistent=n --generate-rcfile > pylint.conf. Looking at pylint.conf, you can see that persistent=no is no longer its default value of yes.
--rcfile= :Specify a configuration file. Put the configuration you use in the configuration file, which not only standardizes your own code, but also allows you to easily share these specifications with others.
-i , --include-ids= : Include the message id in the output, and then use pylint --help-msg= to view the details of this error information so that the error can be specifically located.
-r , --reports= : The default is y, which means that in addition to the source code analysis part, Pylint's output also includes the report part.
--files-output= : Output the message of each module/package to a file named pylint_module/package. [txt|html]. If there is a report, output it to a file named pylint_global. .[txt|html] file. The default is to output to the screen and not to a file.
-f , --output-format= : Set the output format. The formats that can be selected are text, parseable, colorized, msvs (visual studio) and html. The default output format is text.
--disable-msg= : Disable messages with specified ids. For example, the output contains the warning message W0402. If you do not want it to appear in the output, you can use --disable-msg= W0402

6. Pylint output

The default output format of Pylint is raw text format, which can be passed -f , --output-format= to specify other output formats such as html, etc.

There are two parts in the output of Pylint: the source code analysis part and the report part.
Source code analysis part:
For each Python module, the Pylint result first displays some "*" characters, followed by the name of the module, and then a series of messages. The format of the message is as follows: __MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE__ MESSAGE_TYPE has the following types:

(C) Convention. Violation of coding style standards
(R) Refactoring. Very poorly written code.
(W) Warning. Some Python specific questions.
(E) Error. Most likely a bug in the code.
(F) Fatal error. An error that prevents Pylint from running further.

Report part:

After the source code analysis is completed, there will be a series of reports, each report focusing on certain aspects of the project, such as each The number of messages of various categories, module dependencies, etc.
Specifically, the report will include the following aspects: The number of modules checked. For each module, the percentage of errors and warnings.
For example, there are two modules A and B. If a total of 4 errors are detected, 1 error is in A and 3 errors are in B, then the percentage of errors in A is 25%, and the percentage of errors in B is 25%. The percentage is 75%. The total number of errors and warnings.

How to use pylint in python

Under Eclipse IDE, open the Window->Preferences... dialog box and select " PyDev"->"Interpreter Python", click the New button, select Python.exe from the Python installation path, and then a dialog box will pop up asking you to check System PYTHONPATH. I have selected both, so it should not matter. Finally click OK to exit.

1. Development and configuration of Django project

1. Establish PyDev Project

Eclipse IDE——>File——>New— —>Other——>Pydev——>Select Pydev Project——>Name a project (such as demo)——>Select a path, such as E:/work——>Choose the Python that suits you Version (mine is 2.5) -> Uncheck the checkbox below and do not create the src file -> Finish.

2. Create Django Project

(1) django-admin.py startproject demo. (It seems that you cannot use django-admin.py directly on the command line, but must use C:/Python26/Scripts/django-admin.py. Later I learned that you need to add C:/Python26/Scripts to Path.)

(2) Copy the generated Django project directory to the directory under the project just created by Eclipse. Return to the Elicpse IDE just now——> Refresh the newly created project demo in the PyDev Package view to see the Django project. Adding and deleting files and directories can be done through the right-click menu.

3. Django project configuration

(1) Right-click project——>Properties——>Select PyDev-PYTHONPATH——>add source folder (select the project file path to add to Project code——> OK.

(2) Configure the Pydev project:

Select the Pydev project name——>Run as——>Open Run Dialog——>Python Run——>Right-click New——>Write the project name in the Main panel and load it through Browse Project name and Main Module, select manage.py for Main Module——> In the Augment panel, enter runserver --noreload in arguments, and add the following working directory to your base directory.

2. The debugging configuration of the Django project

is similar to the development configuration, but two environment variables are added. The specific operations are as follows:
(1)project Right click——>Debug as——>open Debug dialog.
(2) In the main window, select the project where manage.py is located, and in the Main Module, select the file location where manage.py is located.
(3) Enter runserver --noreload in arguments.
(4)Add DJANGO_SETTINGS_MODULE=settings,PYTHONPATH=$PWD in Environment.

3. Configuring pylint

1. Introduction to pylint

Pylint is mainly used to analyze your PY code, find out the errors, and provide A PYTHON module that gives you hints and can also give you some coding style hints. In short, its function is 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 Code, so that Your code is unified and more readable.

2. Download and install pylint

pylint, logilab-astng, logilab-common. The installation method is the same as that of installing django. That is, just use
python setup.py install directly.

3. Configure the use of pylint
(1)Window -> preferences -> Pydev -> Pylint, select "Use pylint?", and then enter the address of lint.py, such as "C: /Python25/Lib/site-packages/pylint/lint.py"

(2) And add parameters in the last edit box to limit the output of pylint.

                                                                                                                                                                                                                                                                  --persistent=n --comment=n

#         (3)Project->Properties->PyDev-PYTHONPATH Add the project’s source file directory to "Project Source Folders".

          (4) Select Project->Build Automatically, so that pylint will automatically check the code in the project when saving changes, otherwise you have to use Ctrl+B to manually build and trigger pylint.

Reference document:


http://pydev.org/manual_adv_pylint.html

Related recommendations:

How to use Pylint to standardize Python code style (from IBM)_python

python The code inspection tool pylint makes your python more standardized

The above is the detailed content of How to use pylint in python (pylint code inspection)_python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

See all articles