Python 如何使用 PyCharm 运行 Pylint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38134086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:24:27  来源:igfitidea点击:

How to run Pylint with PyCharm

pythonpython-3.xpycharmpylint

提问by Wasif Hyder

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__.pyand without, and its not working either way.

我想将 pylint 配置为我正在处理的 Python 项目的整个项目目录上的外部工具。我尝试将存储库用作模块,__init__.py无论有没有,它都不起作用。

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.

我在设置 pylint 以与 PyCharm 一起运行时遇到了困难。我知道我应该将它作为外部工具运行,但是这些设置让我感到困惑。

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

他们文档中的权威来源已损坏,因此我也无法检查。

回答by lmiguelvargasf

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

您可以pylint按照以下步骤设置使用 PyCharm:

  1. Install pylint:

    $ pip install pylint
    
  2. Locate your pylintinstallation 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 Toolsin 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 Programuse 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 pylintfrom Tools -> External Tools -> pylint:

    PyCharm External Tools

  6. Look your output in the PyCharm terminal

    PyCharm terminal

  1. 安装pylint

    $ pip install pylint
    
  2. 找到您的pylint安装文件夹:

    $ 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. 使用 来打开 PyCharm 设置窗口File -> Settings,然后导航到Tools -> External Tools侧边栏中的 。(或搜索“外部工具”)

    PyCharm 外部工具

  4. 通过单击 + 号并相应地填写字段来设置外部工具。在Program使用运行时,你得到的路径which pylint,为其他值,您可以使用相同的图像。

    PyCharm 编辑工具

  5. 运行pylint来自Tools -> External Tools -> pylint

    PyCharm 外部工具

  6. 在 PyCharm 终端中查看您的输出

    PyCharm 终端

For more details, refer to Pylinting with PyCharm.

有关更多详细信息,请参阅使用 PyCharm进行Pylinting

Update:

更新:

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

如果要用于pylint检查整个项目或特定文件或目录,可以右键单击项目根目录、文件或目录,然后External Tools -> pylint如下所示。

PyCharm check entire project

PyCharm 检查整个项目

回答by lkraider

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

因为我没有找到可用的现成设置,这些是我在 macOS 上的 PyCharm CE 2018.1 中使用的设置:

1 - pip install pylintin your project virtualenv or globally

1 -pip install pylint在您的项目 virtualenv 或全局

2 - Add new external tool and configure:

2 - 添加新的外部工具并配置:

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.

请注意 msg-template 所需的双引号,以及输出过滤器的转义字符。输出过滤器允许单击文件路径并在 IDE 源代码编辑器中打开该位置。

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.

唯一缺少的功能是输出过滤器将 lint 描述直接绘制到源视图中,就像内置的 linter 一样。不过目前没有这样的功能。

pylint arguments

pylint 参数

回答by Roberto Leinardi

You can try this Pylint PyCharm plugin:

你可以试试这个Pylint PyCharm 插件

Pylint PyCharm screenshot

Pylint PyCharm 截图

provides both real-time and on-demand scanning of Python files with Pylint from within PyCharm/IDEA.

在 PyCharm/IDEA 中使用 Pylint 提供 Python 文件的实时和按需扫描。

Once you have it installed, the real-time inspection works automatically. For the on-demand you have several options that go from just checking the current open file to scan the entire project: enter image description here

安装后,实时检查会自动进行。对于点播,您有几个选项,从只检查当前打开的文件到扫描整个项目: 在此处输入图片说明

(Just for the sake of transparency, I am the developer)

(为了透明,我是开发者)

回答by Aaron F

Edit: I now use and recommend the Pycharm pluginwhich didn't exist when I first wrote this answer.

编辑:我现在使用并推荐我第一次写这个答案时不存在的Pycharm 插件



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

一位同事向我介绍了 GitHub 上的pylint-pycharm。它是 pylint 的包装器,输出格式为 PyCharm。这是我如何设置它:

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

This creates build/scripts-2.7/pylint-pycharm

这造成 build/scripts-2.7/pylint-pycharm

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

然后,在 PyCharm 中,使用以下设置创建一个新的外部工具:

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

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.

现在从工具 -> 外部工具 -> PyLintPyCharm 运行它。每行输出都将超链接到源代码中的相关位置。

回答by ub_marco

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.

关于之前答案的注释。我正在寻找一种方法让 PyC​​harm 知道输出语法,这样我就可以直接跳转到文件位置。无需使用其他工具即可工作。

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

可以使用msg-templatepylintrc 文件中的选项或 CLI 选项将 Pylint配置为以特定格式输出消息--msg-template

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

我将其设置为: 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=colorizedso I get this: enter image description herePyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.

在外部工具设置中,Output filters:可以设置为 $FILE_PATH$:$LINE$:$COLUMN$: .*PyCharm 显示链接以直接跳转到报告的位置。这可以结合使用,output-format=colorized所以我明白了: enter image description here尽管配置了该列,PyCharm 仍无法识别该列。但是拥有文件和行对我来说就足够了。

回答by ikreb

At first install pylint with pip:

首先使用 pip 安装 pylint:

pip install pylint

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

您必须在 PyCharm 中打开“设置 > 工具 > 外部工具”并按“+”按钮。

Here are an examplewith good settings.

这是一个具有良好设置的示例

回答by architectonic

Roberto Leinardi has created a pylint plugin for pycharm which really works and integrates well into the IDE:

Roberto Leinardi 为 pycharm 创建了一个 pylint 插件,它确实有效并能很好地集成到 IDE 中:

Easy to install from the repositories, full instructions under: https://github.com/leinardi/pylint-pycharm

易于从存储库安装,完整说明位于:https: //github.com/leinardi/pylint-pycharm

I have a short yet a happy experience with it so far! Kudos to Roberto.

到目前为止,我有一个短暂但愉快的经历!向罗伯托致敬。

回答by Erik

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).

多亏了这里的信息和来自 PyCharm 的更新文档,我已经能够很好地工作,也可以为项目使用虚拟环境(确保任何包都可以部署在虚拟环境中,不需要全局部署)。

Taking what lkraider provided earlier but with slight modifications:

采用 lkraider 之前提供的内容,但稍作修改:

  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$.

  1. 确保在虚拟环境中安装 pylint。请注意,请确保在创建虚拟环境时没有选择“继承全局站点包”。如果你这样做,那么 pylint 将最终成为全球性的,这将不起作用。

  2. 添加新的外部工具并配置。这与 lkraider 提供的略有不同。对于一个,我希望它看起来更像普通的 pylint 输出,因此我的 msg-template(和输出过滤器)有点不同。各有千秋。第二个变化是基于虚拟环境执行 pylint 的更关键的一个,即我使用 $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

pylint 的外部工具