Home >Backend Development >Python Tutorial >How to use pylint in python (pylint code inspection)_python
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
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)_pythonpython 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!