|
2 | 2 | Running Pylint
|
3 | 3 | ================
|
4 | 4 |
|
5 |
| -From the command line |
6 |
| ---------------------- |
| 5 | +On module packages or directories |
| 6 | +--------------------------------- |
7 | 7 |
|
8 | 8 | Pylint is meant to be called from the command line. The usage is ::
|
9 | 9 |
|
10 | 10 | pylint [options] modules_or_packages
|
11 | 11 |
|
12 |
| -You should give Pylint the name of a python package or module, or some number |
13 |
| -of packages or modules. Pylint |
14 |
| -``will not import`` this package or module, though uses Python internals |
15 |
| -to locate them and as such is subject to the same rules and configuration. |
16 |
| -You should pay attention to your ``PYTHONPATH``, since it is a common error |
17 |
| -to analyze an installed version of a module instead of the |
18 |
| -development version. |
19 |
| - |
20 |
| -It is also possible to analyze Python files, with a few |
21 |
| -restrictions. The thing to keep in mind is that Pylint will try to |
22 |
| -convert the file name to a module name, and only be able to process |
23 |
| -the file if it succeeds. :: |
24 |
| - |
25 |
| - pylint mymodule.py |
26 |
| - |
27 |
| -should always work since the current working |
28 |
| -directory is automatically added on top of the python path :: |
29 |
| - |
30 |
| - pylint directory/mymodule.py |
31 |
| - |
32 |
| -will work if ``directory`` is a python package (i.e. has an __init__.py |
33 |
| -file or it is an implicit namespace package) or if "directory" is in the |
34 |
| -python path. |
35 |
| - |
36 |
| -By default, pylint will exit with an error when one of the arguments is a directory which is not |
37 |
| -a python package. In order to run pylint over all modules and packages within the provided |
38 |
| -subtree of a directory, the ``--recursive=y`` option must be provided. |
39 |
| - |
40 |
| -For more details on this see the :ref:`faq`. |
41 |
| - |
42 |
| -From another python program |
43 |
| ---------------------------- |
44 |
| - |
45 |
| -It is also possible to call Pylint from another Python program, |
46 |
| -thanks to the ``Run()`` function in the ``pylint.lint`` module |
47 |
| -(assuming Pylint options are stored in a list of strings ``pylint_options``) as: |
48 |
| - |
49 |
| -.. sourcecode:: python |
50 |
| - |
51 |
| - import pylint.lint |
52 |
| - pylint_opts = ['--disable=line-too-long', 'myfile.py'] |
53 |
| - pylint.lint.Run(pylint_opts) |
54 |
| - |
55 |
| -Another option would be to use the ``run_pylint`` function, which is the same function |
56 |
| -called by the command line. You can either patch ``sys.argv`` or supply arguments yourself: |
57 |
| - |
58 |
| -.. sourcecode:: python |
59 |
| - |
60 |
| - import pylint |
61 |
| - |
62 |
| - sys.argv = ["pylint", "your_file"] |
63 |
| - pylint.run_pylint() |
64 |
| - # Or: |
65 |
| - pylint.run_pylint(argv=["your_file"]) |
| 12 | +By default the ``pylint`` command only accepts a list of python modules and packages. Using a |
| 13 | +directory which is not a package results in an error:: |
66 | 14 |
|
67 |
| -To silently run Pylint on a ``module_name.py`` module, |
68 |
| -and get its standard output and error: |
| 15 | + pylint mydir |
| 16 | + ************* Module mydir |
| 17 | + mydir/__init__.py:1:0: F0010: error while code parsing: Unable to load file mydir/__init__.py: |
| 18 | + [Errno 2] No such file or directory: 'mydir/__init__.py' (parse-error) |
69 | 19 |
|
70 |
| -.. sourcecode:: python |
| 20 | +When ``--recursive=y`` option is used, modules and packages are also accepted as parameters:: |
71 | 21 |
|
72 |
| - from pylint import epylint as lint |
| 22 | + pylint --recursive=y mydir mymodule mypackage |
73 | 23 |
|
74 |
| - (pylint_stdout, pylint_stderr) = lint.py_run('module_name.py', return_std=True) |
| 24 | +This option makes ``pylint`` attempt to discover all modules (files ending with ``.py`` extension) |
| 25 | +and all packages (all directories containing a ``__init__.py`` file). |
75 | 26 |
|
76 |
| -It is also possible to include additional Pylint options in the first argument to ``py_run``: |
77 |
| - |
78 |
| -.. sourcecode:: python |
79 |
| - |
80 |
| - from pylint import epylint as lint |
81 |
| - |
82 |
| - (pylint_stdout, pylint_stderr) = lint.py_run('module_name.py --disable C0114', return_std=True) |
83 |
| - |
84 |
| -The options ``--msg-template="{path}:{line}: {category} ({msg_id}, {symbol}, {obj}) {msg}"`` and |
85 |
| -``--reports=n`` are set implicitly inside the ``epylint`` module. |
86 |
| - |
87 |
| -Finally, it is possible to invoke pylint programmatically with a |
88 |
| -reporter initialized with a custom stream: |
89 |
| - |
90 |
| -.. sourcecode:: python |
91 |
| - |
92 |
| - from io import StringIO |
93 |
| - |
94 |
| - from pylint.lint import Run |
95 |
| - from pylint.reporters.text import TextReporter |
96 |
| - |
97 |
| - pylint_output = StringIO() # Custom open stream |
98 |
| - reporter = TextReporter(pylint_output) |
99 |
| - Run(["test_file.py"], reporter=reporter, do_exit=False) |
100 |
| - print(pylint_output.getvalue()) # Retrieve and print the text report |
101 |
| - |
102 |
| -The reporter can accept any stream object as as parameter. In this example, |
103 |
| -the stream outputs to a file: |
104 |
| - |
105 |
| -.. sourcecode:: python |
106 |
| - |
107 |
| - from pylint.lint import Run |
108 |
| - from pylint.reporters.text import TextReporter |
109 |
| - |
110 |
| - with open("report.out", "w") as f: |
111 |
| - reporter = TextReporter(f) |
112 |
| - Run(["test_file.py"], reporter=reporter, do_exit=False) |
113 |
| - |
114 |
| -This would be useful to capture pylint output in an open stream which |
115 |
| -can be passed onto another program. |
| 27 | +Pylint **will not import** this package or module, though uses Python internals |
| 28 | +to locate them and as such is subject to the same rules and configuration. |
| 29 | +You should pay attention to your ``PYTHONPATH``, since it is a common error |
| 30 | +to analyze an installed version of a module instead of the development version. |
116 | 31 |
|
117 |
| -If your program expects that the files being linted might be edited |
118 |
| -between runs, you will need to clear pylint's inference cache: |
| 32 | +On files |
| 33 | +-------- |
119 | 34 |
|
120 |
| -.. sourcecode:: python |
| 35 | +It is also possible to analyze Python files, with a few restrictions. As a convenience, |
| 36 | +you can give it a file name if it's possible to guess a module name from the file's |
| 37 | +path using the python path. Some examples: |
121 | 38 |
|
122 |
| - from pylint.lint import pylinter |
123 |
| - pylinter.MANAGER.clear_cache() |
| 39 | +``pylint mymodule.py`` should always work since the current working |
| 40 | +directory is automatically added on top of the python path |
124 | 41 |
|
| 42 | +``pylint directory/mymodule.py`` will work if: ``directory`` is a python |
| 43 | +package (i.e. has an ``__init__.py`` file), an implicit namespace package |
| 44 | +or if ``directory`` is in the python path. |
125 | 45 |
|
126 | 46 | Command line options
|
127 | 47 | --------------------
|
|
0 commit comments