【问题标题】:Python correctness (i.e., lint) analyzing for Notepad++为 Notepad++ 分析 Python 正确性(即 lint)
【发布时间】:2011-06-26 15:40:00
【问题描述】:

有没有人知道诸如 pylintpychecker 之类的记事本++?或者也许如何在 notepad++ 中使用 pylint。

【问题讨论】:

    标签: python ide notepad++ pylint pychecker


    【解决方案1】:

    如果你安装了Python Script plugin,那么你可以添加一个带有以下行的新脚本以获得相当好的结果:

    console.show()
    console.clear()
    console.run('cmd.exe /c '
                + 'C:\\Python26\\Scripts\\pylint.bat --reports=n -f parseable '
                + '"%s"' % notepad.getCurrentFilename())
    

    输出将包含指向带有错误/警告的行的超链接(如果文件名中没有空格...)

    【讨论】:

    • 由于从上面的链接到实际下载有点折腾,这里直接链接:sourceforge.net/projects/npppythonscript/files
    • 你必须先安装 Pylint。
    • 不是在插件管理器里吗?然后从notepad++安装它:menu>plugins>plugin manager
    【解决方案2】:

    “-f parseable”选项在当前版本的 Pylint 中已弃用

    当前等效的替代方案是:

    console.run('cmd.exe /c '
            + 'C:\\Python26\\Scripts\\pylint.bat --reports=n '
            + '--msg-template="%s" %s' 
            % ( '{path}:{line}: {msg_id}({symbol}), {obj} {msg}', notepad.getCurrentFilename()))
    

    注意:python 路径可以不同,例如C:\\Python27.

    注意2:--msg-template="..." 中的双引号很重要

    【讨论】:

      【解决方案3】:

      您可以使用 python -m pip install pylint 安装 PyLint 并通过 Notepad++ 的 Run... 命令 (F5) 使用它:

      cmd /c python -m pylint "$(FULL_CURRENT_PATH)" & pause
      

      要在 Notepad++ 中获取输出并链接到代码,use NppExec

      【讨论】:

      • 你能详细解释一下吗?
      • 对于最新的软件(Windows 10、N++ 等),您需要的是:cmd /c pylint "$(FULL_CURRENT_PATH)" & pause
      【解决方案4】:

      其他答案都不适合我,但这确实有效:

      1. 使用python -m pip install pylint 安装 PyLint

      2. 通过Plugin Manager安装NppExec,按F6,并将这个脚本保存为“PyLint”:

         NPP_SAVE
         cd "$(FULL_CURRENT_PATH)"
         env_set PYTHONIOENCODING=utf-8
         python -u -m pylint "$(FULL_CURRENT_PATH)"
        

      样本输出:

      NPP_SAVE: C:\Users\Cees\Documents\http_ear.py
      CD: C:\Users\Cees\Documents\http_ear.py
      Current directory: C:\Users\Cees\Documents
      ENV_SET: PYTHONIOENCODING=utf-8
      $(SYS.PYTHONIOENCODING) = utf-8
      python -u -m pylint "C:\Users\Cees\Documents\http_ear.py"
      Process started (PID=25136) >>>
      ************* Module http_ear
      http_ear.py:16:0: C0301: Line too long (1780/100) (line-too-long)
      http_ear.py:17:0: C0301: Line too long (226/100) (line-too-long)
      http_ear.py:26:0: C0304: Final newline missing (missing-final-newline)
      
      ------------------------------------------------------------------
      Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)
      
      <<< Process finished (PID=25136). (Exit code 16)
      ================ READY ================
      

      您可以使用 NppExec 的控制台输出过滤器链接错误位置。按 Shift+F6 并启用此过滤器,并将红色设置为 FF:

      %FILE%:%LINE%:%CHAR%
      

      然后双击红线在编辑器中聚焦指定位置。

      【讨论】:

        【解决方案5】:

        如果你想在 NotePad++ 中使用 Pylint,你应该使用 Executable 而不是 Batch。

        从 Python 脚本转到配置并创建一个新的 .py 文件以从中运行 Pylint。 (我将我的文件称为 npphelper.py
        将 npphelper.py 添加到菜单项和工具栏图标中,然后您可以通过按按钮来执行它。

        这会将 Pylint 运行到 Notepad++ 中,我将命令分成 2 个部分:

        pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n'
        console.show()
        console.clear()
        console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
        
        1. pylint.exe 的路径(我使用的是短名称而不是双引号
        2. 您要使用 Pylint 检查的文件(实际上从活动选项卡返回路径

        您必须更改路径以使其适合您的安装...

        你现在要做的就是保存这个 npphelper.py,用你的项目文件打开选项卡并运行你为 pylint 创建的 npphelper.py。 (例如通过按钮


        如果您不想使用默认配置,则生成一个 pylintrc 模板(将它们保存在您想要的位置)。我已经通过 CMD 使用以下命令完成了它:

        pylint.exe --generate-rcfile>>myfilename.pylintrc
        

        那你需要在npphelper.py中改几行:

        rcfile = 'C:\\PROGRA~1\\Python35\\Scripts\\myrcfile.pylintrc'
        pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n --rcfile="%s"' % rcfile
        console.show()
        console.clear()
        console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
        

        我已经使用 .msi 文件 here 安装了 Python Script 1.0.8.0 和所有 Extras。
        在 Notepad++ 中使用 PluginManager 会为您提供 1.0.6.0 版本而不是 1.0.8.0

        我使用 Windows 7 和 Notepad++ 6.9.1、Python 3.5.1 和 Pylint 1.5.5。
        我通过 CMD 安装了 pylint -> "pip install pylint" 并更新了它。


        一些更有用的链接:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-30
          • 1970-01-01
          相关资源
          最近更新 更多