How to run Pylint with PyCharm

2019-01-30 18:11发布

I want to configure pylint as an external tool on my entire project directory for a python project that I'm working on. I've tried to use the repository as a module with __init__.py and without, and its not working either way.

I'm having difficulty setting up pylint to run with PyCharm. I know that I should be running it as an external tool, however the settings confuse me.

The authoritative source on their documentation is broken, so I can't check that up either.

7条回答
ら.Afraid
2楼-- · 2019-01-30 18:43

Because I didn't find a working ready-made setup, these are the settings I use in PyCharm CE 2018.1 on macOS:

1 - pip install pylint in your project virtualenv or globally

2 - Add new external tool and configure:

Program: pylint
Arguments: "--msg-template='{abspath}:{line:5d},{column:2d}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\,\s*$COLUMN$:

Notice the required double quotes for the msg-template, and the escape chars for the output filters. The output filter allows to click on the file path and open the location in the IDE source editor.

Only missing feature would be the output filters to plot the lint descriptions directly into the source view, as is done with the builtin linter. No such feature at this time though.

pylint arguments

查看更多
男人必须洒脱
3楼-- · 2019-01-30 18:45

A colleague pointed me towards pylint-pycharm on GitHub. It's a wrapper around pylint with output formatted for PyCharm. Here's how I set it up:

git clone https://github.com/perses76/pylint-pycharm.git
cd pylint-pycharm
python setup.py build

This creates build/scripts-2.7/pylint-pycharm

Then, in PyCharm, create a new External Tool with these settings:

Program: path to your installation of pylint-pycharm  
Arguments: --virtualenv=$PyInterpreterDirectory$/.. $FileName$
Working directory: $FileDir$ 
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*

PyLint for PyCharm External Tool settings screenshot

Now run it from Tools -> External Tools -> PyLintPyCharm. Each line of output will be hyperlinked to the relevant position in the source code.

查看更多
爷、活的狠高调
4楼-- · 2019-01-30 18:45

A note on the previous answers. I was searching for a method to make PyCharm aware of the output syntax so I can directly jump to the file locations. That works without using additional tools.

Pylint can be configured to output messages in a specific format using the msg-template option in the pylintrc file or the CLI option --msg-template.

I set it to: msg-template='{abspath}:{line}:{column}: {msg_id} {msg}'

In the External Tools settings, the Output filters: can be set to $FILE_PATH$:$LINE$:$COLUMN$: .* so PyCharm shows links to directly jump to the reported locations. This can be combined with output-format=colorized so I get this: enter image description here PyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.

查看更多
成全新的幸福
5楼-- · 2019-01-30 18:49

At first install pylint with pip:

pip install pylint

You have to open “Settings > Tools > External Tools” and press the “+” button at PyCharm.

Here are an example with good settings.

查看更多
萌系小妹纸
6楼-- · 2019-01-30 18:55

Thanks to information here, and updated documentation from PyCharm I've been able to get this to work nicely to also use the virtual environment for the project (ensuring that any packages can be deployed within the virtual environment and do not need to be deployed globally).

Taking what lkraider provided earlier but with slight modifications:

  1. Ensure you install pylint within the virtual environment. Note, make sure that when you created the virtual environment you did not select "Inherit global site-packages". If you do then pylint will end up being globally and this will not work.

  2. Add new external tool and configure. This is slightly different compared to what lkraider provided. For one I wanted it to look more like normal pylint output hence my msg-template (and Output filter) is a bit different. Each to their own. Second change is more critical one for executing pylint based on the virtual environment, that is the program parameter where I use $PyInterpreterDirectory$.

Program: $PyInterpreterDirectory$/pylint
Arguments: "--msg-template='{abspath}:{line:5d}:{column}: {msg_id}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\:\s*$COLUMN$:

External tool for pylint

查看更多
戒情不戒烟
7楼-- · 2019-01-30 18:56

You can set up pylint to work with PyCharm by following the next steps:

  1. Install pylint:

    $ pip install pylint
    
  2. Locate your pylint installation folder:

    $ which pylint         # MacOS/Linux
    /usr/local/bin/pylint  # this is just a possible output check yours
    

    $ where pylint         # Windows
    %LocalAppData%\Programs\Python\Python36-32\Scripts\pylint.exe  # possible location
    
  3. Open the PyCharm settings window with File -> Settings, then navigate to Tools -> External Tools in the sidebar. (Or search "external tools")

    PyCharm External tools

  4. Setup an external tool by clicking on the + sign and filling the fields accordingly. In Program use the path you got when running which pylint, for the other values you can use the same of the image.

    PyCharm Edit Tool

  5. Run pylint from Tools -> External Tools -> pylint:

    PyCharm External Tools

  6. Look your output in the PyCharm terminal

    PyCharm terminal

For more details, refer to Pylinting with PyCharm.

Update:

If you want to use pylint to check your whole project or a particular file or directory, you can right click on your project root, file or directory, then External Tools -> pylint as shown below.

PyCharm check entire project

查看更多
登录 后发表回答