【问题标题】:How to use Pylint or other linters with Jupyter Notebooks?如何在 Jupyter Notebooks 中使用 Pylint 或其他 linter?
【发布时间】:2020-02-01 02:16:37
【问题描述】:

是否可以在 Jupyter Notebook 中使用 linter?

【问题讨论】:

    标签: python jupyter-notebook pylint linter


    【解决方案1】:
    • 是的,有可能
    • 您可以为 Jupyter Notebook 安装pycodestyle,类似于pylint。 您可以在 Jupyter Notebook shell 中使用以下命令:
    # install
    !pip install pycodestyle pycodestyle_magic
    
    
    # load
    %load_ext pycodestyle_magic
    
    
    # use
    %%pycodestyle
    def square_of_number(
         num1, num2, num3, 
         num4):
        return num1**2, num2**2, num3*
    
    # Output
    2:1: E302 expected 2 blank lines, found 0
    3:23: W291 trailing whitespace
    
    
    • 查看this 问题了解更多详情。我的回答灵感来自链接中的答案。
    • 查看this 问题,了解更多类似的短绒和解决方案。

    【讨论】:

      【解决方案2】:

      您可以使用 FlakeHell 运行 flake8 on entire notebooks 支持的任意数量的 linter

      【讨论】:

        【解决方案3】:

        是的 - 您可以使用 nbQA 在 Jupyter Notebook 上运行任何标准 Python 代码质量工具

        例如:

        pip install -U nbqa pylint
        nbqa pylint notebook.ipynb
        

        免责声明:我是 nbQA 的作者

        【讨论】:

          【解决方案4】:

          如果你想在 Jupyter 中使用 black linter:

          pip install black "black[jupyter]"
          black {source_file_or_directory}
          

          如果您想使用 pre-commit 挂钩自动 lint 您的笔记本,您必须将 id: black 替换为 id: black-jupyter (more info here)。

          【讨论】:

            猜你喜欢
            • 2021-07-17
            • 2019-12-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-25
            • 2019-04-11
            • 1970-01-01
            • 2021-12-02
            相关资源
            最近更新 更多